Skip to content

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.

License

Notifications You must be signed in to change notification settings

alexinator1/yii2-jta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

##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

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages