Download the PHP package execut/yii2-save-relations-behavior without Composer

On this page you can find all versions of the php package execut/yii2-save-relations-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 yii2-save-relations-behavior

Yii2 Active Record Save Relations Behavior

Automatically validate and save related Active Record models.

Latest Stable Version Total Downloads Code Coverage Build Status Latest Unstable Version License

Features

Installation

The preferred way to install this extension is through composer.

Either run

or add

to the require section of your composer.json file.

Configuring

Configure model as follows

Though not mandatory, it is highly recommended to activate the transactions for the owner model.

Usage

Every declared relations in the relations behavior parameter can now be set and saved as follow:

You can set related model by only specifying its primary key:

You can even set related models as associative arrays like this:

Attributes of the related model will be massively assigned using the `load() method. So remember to declare the according attributes as safe in the rules of the related model.

Note: Only newly created or changed related models will be saved.

See the PHPUnit tests for more examples.

Populate additional junction table columns in a many-to-many relation

In a many-to-many relation involving a junction table additional column values can be saved to the junction table for each model. See the configuration section for examples.

Note: If junction table properties are configured for a relation the rows associated with the related models in the junction table will be deleted and inserted again on each saving to ensure that changes to the junction table properties are saved too.

Validation

Every declared related models will be validated prior to be saved. If any validation fails, for each related model attribute in error, an error associated with the named relation will be added to the owner model.

For hasMany() relations, the index of the related model will be used to identify the associated error message.

It is possible to specify the validation scenario for each relation by declaring an associative array in which the scenario key must contain the needed scenario value. For instance, in the following configuration, the links related records will be validated using the Link::SOME_SCENARIO scenario:

It is also possible to set a relation scenario at runtime using the setRelationScenario as follow:

Tips: For relations not involving a junction table by using the via() or viaTable() methods, you should remove the attributes pointing to the owner model from the 'required' validation rules to be able to pass the validations.

Note: If an error occurs for any reason during the saving process of related records in the afterSave event, a yii\db\Exception will be thrown on the first occurring error. An error message will be attached to the relation attribute of the owner model. In order to be able to handle these cases in a user-friendly way, one will have to catch yii\db\Exception exceptions.

Delete related records when the main model is deleted

For DBMs with no built in relational constraints, as of 1.5.0 release, one can now specify a relation to be deleted along with the main model.

To do so, the relation should be declared with a property cascadeDelete set to true. For example, related projectLinks records will automaticaly be deleted when the main model will be deleted:

Note: Every records related to the main model as they are defined in their ActiveQuery statement will be deleted.

Populate the model and its relations with input data

This behavior adds a convenient method to load relations models attributes in the same way that the load() method does. Simply call the loadRelations() with the according input data.

For instance:

You can even further simplify the process by adding the SaveRelationsTrait to your model. In that case, a call to the load() method will also automatically trigger a call to the loadRelations() method by using the same data, so you basically won't have to change your controllers.

The relationKeyName property can be used to decide how the relations data will be retrieved from the data parameter.

Possible constants values are:

Get old relations values

To retrieve relations value prior to there most recent modification until the model is saved, the following methods can be used:

Notes

  • If a relation has not been modified yet, its initial value will be returned
  • Only relations defined in the behavior parameters will be returned

Get dirty relations

To deal with dirty (modified) relations since the model was loaded, the following methods can be used:


All versions of yii2-save-relations-behavior with dependencies

PHP Build Version
Package Version
Requires yiisoft/yii2 Version >=2.0.14
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 execut/yii2-save-relations-behavior contains the following files

Loading the files please wait ....