2017 © Pedro Peláez
 

yii2-extension yii2-jta

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

image

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

  • Thursday, February 2, 2017
  • by alexinator1
  • Repository
  • 2 Watchers
  • 4 Stars
  • 607 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 9 Versions
  • 17 % Grown

The README.md

##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., (*1)

Requirements

  • Yii 2.0
  • PHP 5.4

Installation

The preferred way to install this extension is through Composer., (*2)

Either run, (*3)

$ composer require alexinator1/yii2-jta

or add, (*4)

"alexinator1/yii2-jta": "*"

to the require section of your composer.json file., (*5)

Usage

Just inherit your both model classes related in many-to-many relation from alexinator1\jta\ActiveRecord class., (*6)

Consider following scheme, (*7)

Database scheme for example, (*8)

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, (*9)

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:, (*10)

Lazy loading:, (*11)

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

Eager loading:, (*12)

    $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:, (*13)

    $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:, (*14)

    $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, (*15)

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., (*16)

License

**yii2 junction table attributes ** is released under the MIT License. See the bundled LICENSE.md for details., (*17)

Resources

The Versions

02/02 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes

30/01 2017

v0.3

0.3.0.0

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes

08/10 2016

v0.2.4

0.2.4.0

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes

08/10 2016

v0.2.3

0.2.3.0

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes

18/04 2016

v0.2.2

0.2.2.0

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes

18/04 2016

dev-dev

dev-dev

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes

18/04 2016

v0.2.1

0.2.1.0

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes

17/04 2016

v0.2

0.2.0.0

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes

13/09 2015

v0.1

0.1.0.0

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

  Sources   Download

MIT

The Requires

 

by Aleksey Rudakov

yii2 activerecord many-to-many junction table pivot table junction attributes