Skip to content

E96/yii2-relational-behavior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yii2 Relational behavior

This behavior allows you to set relations in your code. Also, it saves many-to-many relations declared with via() or viaTable()

Relations accepts int|int[]|ActiveRecord|ActiveRecord[].

Install

php composer.phar require e96/yii2-relational-behavior:dev-master

Setup

In model:

class User extends ActiveRecord
{
    public function behaviors()
    {
        return [
            e96\behavior\RelationalBehavior::className(), // enable behavior
        ];
    }

    public function rules()
    {
        return [
            ['permissions', 'safe'], // allow set permissions with setAttributes()
        ];
    }

    // define many-to-many relation
    public function getPermissions()
    {
        return $this->hasMany(Permission::className(), ['id' => 'permissionId'])
            ->viaTable('user-map-permission', ['userId' => 'id']);
    }
}

In view:

$form->field($model, 'permissions')->dropDownList($permissions, ['multiple' => true])

Usage

$user->load(Yii::$app->request->post());
$user->save();

Other usages:

$user = User::findOne(1);
$user->permissions = 1;
// or
$user->permissions = [1,2];
// or
$user->permissions = Permission::findOne(1);
// or
$user->permissions = Permission::find()->all();
// or
$user->load(Yii::$app->request->post());
// or
$user->setAttributes(['permissions' => [1,2]]);
// then
$user->save();

Written with StackEdit.

About

Easy saving relational models in yii2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages