alexinator1/yii2-jta Yii2 extension allows to access attributes of junction table and attach them as child model as properties in many-to-many relation

activerecordmany-to-manyjunctiontablepivottablejunctionattributes

Yii2 Junction Table Attributes

================= A simple extension allows to access column values of junction table in ORM way without declaring additional model for that table in many-to-many relation. Extension overwrites \yii\db\ActiveQuery::viaTable() and allows to pass there array of column names of junction table which will be attached to child models as properties.

Requirements

  • Yii 2.0
  • PHP 5.4

Installation

The preferred way to install this extension is through Composer.

Either run

$ composer require alexinator1/yii2-jta

or add

"alexinator1/yii2-jta": "*"

to the require section of your composer.json file.

Usage

Just inherit your both model classes related in many-to-many relation from alexinator1\jta\ActiveRecord class.

Consider following scheme

Database scheme for example

class User extends \alexinator1\jta\ActiveRecord
{
    ....
}
class Group extends \alexinator1\jta\ActiveRecord
{
    ....
}

and pass array of attribute names you want to attach to child model to viaTable method

class Group extends \alexinator1\jta\ActiveRecord
{
    ...

    public function getUsers()
    {
        return $this->hasMany(User::className(), ['id' => 'user_id'])
            ->viaTable('user_group', ['group_id' => 'id'], null, ['role', 'joined_at']);
    }

    ...
}

That's it. Now child model has a property which named as pivot attribute and contains array of corresponding values indexed by parent id. So it can't be accessed following way:

Lazy loading:

    $group = Group::findOne($groupId);
    foreach($group->users as $user)
    {
        $role = $user->role[$group->id];
        $joinDate = $user->joined_at[$group->id];
        ...
    }

Eager loading:

    $group = Group::find()->where($groupId)->with('users')->all();
    foreach($group->users as $user)
    {
        $role = $user->role[$group->id];
        $joinDate = $user->joined_at[$group->id];
        ...
    }

Eager loading using 'joinWith' methods:

    $groups =  Group::find()->joinWith('users')->all();

    foreach($groups as $group){
        foreach($group->users as $user)
        {
            $role = $user->role[$group->id];
            ...
        }
    }

works with 'array' models as well:

    $group = Group::find()
        ->with('users')
        ->where($groupId)
        ->asArray()
        ->one();
    foreach($group['users'] as $user)
    {
        $role = $user['role'];
        $joinDate = $user['joined_at'];
        ...
    }

Show case page!

You may find more code samples in DEMO page. And some unit tests on DEMO page repository

Note!

Attached pivot attributes are read-only and acceptable only for models 
were populated via relation. They overwrite all other none-declared model properties
(declared via getter or corresponded to table columns)
and are overwritten by declared properties.

Failed use cases

If you find any usecases where extension doesn't work properly. Please feel free to issue it or send me to email. We will try to handle it ASAP.

License

yii2 junction table attributes is released under the MIT License. See the bundled LICENSE.md for details.

Resources

Statistics

Downloads
GitHub Stars
GitHub Forks

Releases

Comments



v0.3 is the latest of 7 releases



MIT license
Stats
6 github stars & 4 github forks
2 downloads in the last day
106 downloads in the last 30 days
5432 total downloads