Download the PHP package voskobovich/yii2-many-many-behavior without Composer

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

Yii2 ManyToMany Behavior

This behavior soon will be DEPRECATED.
See the new version Yii2 Linker Behavior.

About

This behavior makes it easy to maintain many-to-many and one-to-many relations in your ActiveRecord models.

License Latest Stable Version Latest Unstable Version Total Downloads Build Status

Support

GutHub issues.

Usage

  1. In your model, add the behavior and configure it
  2. In your model, add validation rules for the attributes created by the behavior
  3. In your view, create form fields for the attributes

Adding and configuring the behavior

As an example, let's assume you are dealing with entities like Book, Author and Review. The Book model has the following relationships:

In the same model, the behaviour can be configured like so:

Relation names don't need to end in _ids, and you can use any name for a relation. It is recommended to use meaningful names, though.

Adding validation rules

The attributes are created automatically. However, you must supply a validation rule for them (usually a safe validator):

Creating form fields

By default, the behavior will accept data from a multiselect field:

Known issues and limitations

Custom getters and setters

Attributes like author_ids and review_ids in the Book model are created automatically. By default, they are configured to accept data from a standard select input (see below). However, it is possible to use custom getter and setter functions, which may be useful for interaction with more complex frontend scripts. It is possible to define many alternative getters and setters for a given attribute:

Field name is concatenated to the attribute name with an underscore. In this example, accessing $model->author_ids will result in an array of IDs, $model->author_ids_json will return a JSON string and $model->author_ids_string will return a comma-separated string of IDs. Setters work similarly.

Getters and setters may be ommitted to fall back to default behavior (arrays of IDs).

NOTE

The setter function receives whatever data comes through the $_REQUEST and is expected to return the array of the related model IDs. The getter function receives the array of the related model IDs.

COMPATIBILITY NOTE

Specifying getters and setters for the primary attribute (author_ids in the above example) is still supported, but not recommended. Best practice is to use primary attribute to get and set values as array of IDs and create fields to use other getters and setters.

Custom junction table values

For seting additional values in junction table (apart columns required for relation), you can use viaTableValues:

Setting default values for orphaned models

When one-to-many relations are saved, old links are removed and new links are created. To remove an old link, the corresponding foreign-key column is set to a certain value. It is NULL by default, but can be configured differently. Note that your database must support your chosen default value, so if you are using NULL as a default value, the field must be nullable.

You can supply a constant value like so:

It is also possible to assign the default value to NULL explicitly, like so: 'default' => null. Another option is to provide a function to calculate the default value:

The function accepts 3 parameters. In our example $model is the instance of the Book class (owner of the behavior), $relationName is 'reviews' and $attributeName is 'review_ids'.

If you need the db connection inside this function, it is recommended to obtain it from either the primary model (Book) or the secondary model (Review).

Applying the behaviour several times to a single relationship

It is possible to use this behavior for a single relationship multiple times in a single model. This is not recommended, however.

Using the behaviour with relations that are using the same junction table

When you are implementing multiple ManyToMany relations in the same model, and they are using same junction table, you may face and issue when your junction records will not be saved properly.

This happens because old junction records are dropped each time new relation is saved. To avoid deletion of records that were just saved, you will need to set customDeleteCondition param.

This delete condition will be merged with primary delete condition and may be used to fine tune your delete query.

For example, let's imagine that we develop a scientific database for botanical laboratory. We have a model called "Sample" for different plants samples, model called "Attachment" for related files (photos or documents) and junction table "sample_attachments". And we want to divide all those files into separate fields in the "Sample" model (raw material pictures, molecular structure, etc) by introducing field "type" in the junction table. In such case, the resulting "Sample" model will look like this:

Installation

The preferred way to install this extension is through composer.

Either run

or add

to the require section of your composer.json file.


All versions of yii2-many-many-behavior with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4.0
yiisoft/yii2 Version ~2.0.0
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 voskobovich/yii2-many-many-behavior contains the following files

Loading the files please wait ....