lucianolima00 / yii2-many-to-many
ManyToMany Relation for Yii2 Framework
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- phpunit/dbunit: ~4.0
- phpunit/phpunit: ~7.1
README
Installation
The preferred way to install this extension is through composer.
Either run
composer require lucianolima00/yii2-many-to-many "*"
or add
"lucianolima00/yii2-many-to-many": "*"
to the require section of your composer.json
file.
Usage
Once the extension is installed, simply use it in your code by:
//app\models\User.php <?php use lucianolima00\ManyToMany\behaviors\ManyToManyBehavior; class User extends \yii\db\ActiveRecord { /** * {@inheritDoc} */ public function behaviors() { return [ [ 'class' => ManyToManyBehavior::class(), 'ownAttribute' => 'user_id', // Name of the column in junction table that represents current model 'relatedModel' => UserTest::class, // Junction model class 'attribute' => 'tests', // Represent the attribute of current model 'relatedAttribute' => 'test_id', // Name of the column in junction table that represents related model 'unique' => true // Ensure that for the same ownAttribute only exist one relatedAttribute with same value. Default is true ], ]; }
For display related models (requires additional model for junction table):
/** * Gets query for [[UserTests]]. * * @return \yii\db\ActiveQuery */ public function getUserTests() { return $this->hasMany(UserTest::class(), ['user_id' => 'id']); }