Download the PHP package yiiext/activerecord-relation-behavior without Composer

On this page you can find all versions of the php package yiiext/activerecord-relation-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package activerecord-relation-behavior

ActiveRecord Relation Behavior Build Status

This extension is inspired by all the yii extensions that aim to improve saving of related records. It allows you to assign related records especially for MANY_MANY relations more easily. It puts together the awesomeness of all the extensions mentionend below (see headline "Feature comparison"). It comes with 100% test coverage and well structured and clean code so it can savely be used in enterprise production enviroment.

Requirements

How to install

  1. Get the source in one of the following ways:
    • Download the latest version and place the files under extensions/yiiext/behaviors/activerecord-relation/ under your application root directory.
    • Add this repository as a git submodule to your repository by calling git submodule add https://github.com/yiiext/activerecord-relation-behavior.git extensions/yiiext/behaviors/activerecord-relation
  2. Add it to the models you want to use it with, by adding it to the behaviors() method.

Let the magic begin...

We have two ActiveRecord classes (the ones from Yii definitive guide):

Somewhere in our application code we can do:

You can also…

Enable/Disable/Reset the relations to save. To do this you can use:

$model->withoutRelations('relation name 1', 'relation name 2',…)->save()

…will save all $model relations except the ones passed to the scope.

$model->withRelations('relation name 1', 'relation name 2',…)->save()

…will save only the $model relations passed to the scope

$model->resetRelations()->save()

…will clear the former scopes, and save all the relations of $model

Some things you should care about...

Exceptions explained

"You can not save a record that has new related records!"

You have assigned a record to a relation which has not been saved (it is not in the database yet). Since ActiveRecord Relation Behavior needs its primary key to save it to a relation table, this will not work. You have to call ->save() on all new records before saving the related record.

"A HAS_MANY/MANY_MANY relation needs to be an array of records or primary keys!"

You can only assing arrays to HAS_MANY and MANY_MANY relations, assigning a single record to a ..._MANY relation is not possible.

"Related record with primary key "X" does not exist!"

You tried to assign primary key value X to a relation, but X does not exist in your database.

Feature comparison

Inspired by and put together the awesomeness of the following yii extensions:

these are the extensions mentioned above

reviewed but did not take something out:

Many thanks to the authors of these extensions for inpiration and ideas.

Run the unit test

This behavior is covered by unit tests with 100% code coverage (ECompositeDbCriteria is currently not covered since composite pks are not fully supported yet). To run the unit tests you need phpunit installed and the test class requires php 5.3 or above.

  1. make sure yii framework is available under ./yii/framework you can do this by
    • cloning the yii git repo with git clone https://github.com/yiisoft/yii.git yii
    • or linking existing yii directory here with ln -s ../../path/to/yii yii
  2. run phpunit EActiveRecordRelationBehaviorTest.php or if you want coverage information in html, run phpunit --coverage-html tmp/coverage EActiveRecordRelationBehaviorTest.php

FAQ

When using a MANY_MANY relation, not changing it in any way and doing save() does it re-save relations or not?

It uses CActiveRecord::hasRelated() to check if a relation has been loaded or set and will only save if this is the case. It will re-save if you loaded and did not change, since it is not able to detect this. But re-saving does not mean entries in MANY_MANY table get deleted and re-inserted. It will only run a delete query, that does not match any rows if you did not touch records, so no row in db will be touched.

is it possible to save only related links (n-m table records) without re-saving model?

Currently not, will add this feature in the future: issue #16.

how can I delete a particular id from many-many relation? do I need to load all related records for this?

Currently you have to load all and re-assign the array. Will add an api for this; issue #16.


All versions of activerecord-relation-behavior with dependencies

PHP Build Version
Package Version
Requires php Version >=5.1.0
yiisoft/yii Version >=1.1.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package yiiext/activerecord-relation-behavior contains the following files

Loading the files please wait ....