PHPackages                             alexinator1/yii2-jta - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. alexinator1/yii2-jta

ActiveYii2-extension[Database &amp; ORM](/categories/database)

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

v0.3(9y ago)77.4k↓33.3%5[1 issues](https://github.com/alexinator1/yii2-jta/issues)[1 PRs](https://github.com/alexinator1/yii2-jta/pulls)MITPHP

Since Sep 13Pushed 9y ago2 watchersCompare

[ Source](https://github.com/alexinator1/yii2-jta)[ Packagist](https://packagist.org/packages/alexinator1/yii2-jta)[ RSS](/packages/alexinator1-yii2-jta/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (9)Used By (0)

\##Yii2 Junction Table Attributes
=================================

[](#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
------------

[](#requirements)

- Yii 2.0
- PHP 5.4

Installation
------------

[](#installation)

The preferred way to install this extension is through [Composer](http://getcomposer.org/).

Either run

```
$ composer require alexinator1/yii2-jta
```

or add

```
"alexinator1/yii2-jta": "*"
```

to the `require` section of your `composer.json` file.

Usage
-----

[](#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](https://cloud.githubusercontent.com/assets/1975274/14590078/a2b66f8c-04fa-11e6-9b64-b861b430de7e.png)](https://cloud.githubusercontent.com/assets/1975274/14590078/a2b66f8c-04fa-11e6-9b64-b861b430de7e.png)

```
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!

[](#show-case-page)

You may find more code samples in [DEMO](http://ec2-35-157-96-249.eu-central-1.compute.amazonaws.com/) page. And some unit tests on [DEMO page repository](https://github.com/alexinator1/jta-demo-app)

#### Note!

[](#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
----------------

[](#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
-------

[](#license)

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

Resources
---------

[](#resources)

- [Source Code](https://github.com/alexinator1/yii2-jta)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~84 days

Recently: every ~72 days

Total

7

Last Release

3389d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1975274?v=4)[alexinator1](/maintainers/alexinator1)[@alexinator1](https://github.com/alexinator1)

---

Top Contributors

[![alexinator1](https://avatars.githubusercontent.com/u/1975274?v=4)](https://github.com/alexinator1 "alexinator1 (13 commits)")

---

Tags

yii2activerecordmany to manyPivot tableJunction tableJunction Attributes

### Embed Badge

![Health badge](/badges/alexinator1-yii2-jta/health.svg)

```
[![Health](https://phpackages.com/badges/alexinator1-yii2-jta/health.svg)](https://phpackages.com/packages/alexinator1-yii2-jta)
```

###  Alternatives

[voskobovich/yii2-linker-behavior

This behavior makes it easy to maintain many-to-many and one-to-many relations in your ActiveRecord models.

80319.0k9](/packages/voskobovich-yii2-linker-behavior)[nhkey/yii2-activerecord-history

Storage history of changes to ActiveRecord

46143.4k1](/packages/nhkey-yii2-activerecord-history)[spinitron/yii2-dynamic-ar

Extends Yii ActiveRecord for Maria Dynamic Columns

576.8k](/packages/spinitron-yii2-dynamic-ar)[jlorente/yii2-activerecord-inheritance

ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework. It fakes inheritance between two ActiveRecord classes.

184.2k](/packages/jlorente-yii2-activerecord-inheritance)[execut/yii2-1c-odata

Yii2 component for work with 1C oData via activeRecord

101.3k](/packages/execut-yii2-1c-odata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
