Download the PHP package yii2tech/ar-linkmany without Composer
On this page you can find all versions of the php package yii2tech/ar-linkmany. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yii2tech/ar-linkmany
More information about yii2tech/ar-linkmany
Files in yii2tech/ar-linkmany
Package ar-linkmany
Short Description Provides support for many-to-many relations saving in Yii2 ActiveRecord
License BSD-3-Clause
Informations about the package ar-linkmany
ActiveRecord Many-to-Many Saving Extension for Yii2
This extension provides support for ActiveRecord many-to-many relation saving. For example: single "item" may belong to several "groups", each group may be linked with several items. Unlike regular [[\yii\db\BaseActiveRecord::link()]] usage, this extension automatically checks references existence, removes excluded references and provide support for web form composition.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json.
Usage
This extension provides support for ActiveRecord many-to-many relation saving. This support is granted via [[\yii2tech\ar\linkmany\LinkManyBehavior]] ActiveRecord behavior. You'll need to attach it to your ActiveRecord class and point the target "has-many" relation for it:
Being attached [[\yii2tech\ar\linkmany\LinkManyBehavior]] adds a virtual proprty to the owner ActiveRecord, which name is determined by [[\yii2tech\ar\linkmany\LinkManyBehavior::$relationReferenceAttribute]]. You will be able to specify related models primary keys via this attribute:
The above example is equal to the following code:
Attention: do NOT declare
relationReferenceAttribute
attribute in the owner ActiveRecord class. Make sure it does not conflict with any existing owner field or virtual property.
Virtual property declared via relationReferenceAttribute
serves not only for saving. It also contains existing references
for the relation:
You may as well edit the references list for existing record, while saving linked records will be synchronized:
Note: if attribute declared by
relationReferenceAttribute
is never invoked for reading or writing, it will not be processed on owner saving. Thus it will not affect pure owner saving.
Creating relation setup web interface
The main purpose of [[\yii2tech\ar\linkmany\LinkManyBehavior::$relationReferenceAttribute]] is support for creating many-to-many setup web interface. All you need to do is declare a validation rule for this virtual property in your ActiveRecord, so its value can be collected from the request:
Inside the view file you should use relationReferenceAttribute
property as an attribute name for the form input:
Inside the controller you don't need any special code: