PHPackages                             stesi/yii2-relation-trait - 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. stesi/yii2-relation-trait

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

stesi/yii2-relation-trait
=========================

Yii 2 Models load with relation, &amp; transaction save with relation

1.1.5.2(8y ago)0881BSD-3-ClausePHPPHP &gt;=5.4.0

Since Jun 3Pushed 8y ago1 watchersCompare

[ Source](https://github.com/stesi/yii2-relation-trait)[ Packagist](https://packagist.org/packages/stesi/yii2-relation-trait)[ Docs](https://github.com/stesi/yii2-relation-trait)[ RSS](/packages/stesi-yii2-relation-trait/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)Dependencies (1)Versions (19)Used By (0)

yii2-relation-trait
===================

[](#yii2-relation-trait)

Yii 2 Models add functionality for load with relation (loadAll($POST)), &amp; transactional save with relation (saveAll())

[![Latest Stable Version](https://camo.githubusercontent.com/366ffb3b3f5c34273dd39c65a034e3229d12afde159e68ce0aff2c1056c3a19d/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6f74656e7361692f796969322d72656c6174696f6e2d74726169742f762f737461626c65)](https://packagist.org/packages/mootensai/yii2-relation-trait)[![License](https://camo.githubusercontent.com/5522e540b024019bf1250a4459aaf2e1e17d0a7bf798f94ada4f57fe7040b62f/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6f74656e7361692f796969322d72656c6174696f6e2d74726169742f6c6963656e7365)](https://packagist.org/packages/mootensai/yii2-relation-trait)[![Total Downloads](https://camo.githubusercontent.com/54c147d3cbccbd5432b1621da8d84e41ff36d3114600c949e141e7adb5fc37fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f6f74656e7361692f796969322d72656c6174696f6e2d74726169742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mootensai/yii2-relation-trait)[![Monthly Downloads](https://camo.githubusercontent.com/d46462162c5966be35efb96cafaef6728645a4677b36bc970b380184ca2fdbbd/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6f74656e7361692f796969322d72656c6174696f6e2d74726169742f642f6d6f6e74686c79)](https://packagist.org/packages/mootensai/yii2-relation-trait)[![Daily Downloads](https://camo.githubusercontent.com/e1fab2c19a06679a5ed7841111890c55e5b75371c3aeccdaaf9dc0d443d10c78/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6f74656e7361692f796969322d72656c6174696f6e2d74726169742f642f6461696c79)](https://packagist.org/packages/mootensai/yii2-relation-trait)[![Join the chat at https://gitter.im/mootensai/yii2-relation-trait](https://camo.githubusercontent.com/abe08b740a4156153736f791393ec4da6619c4be73212e75769f52edacc0e2b5/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667)](https://gitter.im/mootensai/yii2-relation-trait?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)[![Bitdeli Badge](https://camo.githubusercontent.com/9ecdfa9e61878e0105e80cab079dbffe155df43f867b2e1887f6115b84b656d9/68747470733a2f2f64327765637a68766c38323376302e636c6f756466726f6e742e6e65742f6d6f6f74656e7361692f796969322d72656c6174696f6e2d74726169742f7472656e642e706e67)](https://bitdeli.com/free "Bitdeli Badge")

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

[](#installation)

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

Either run

```
$ composer require 'stesi/yii2-relation-trait:dev-master'
```

or add

```
"stesi/yii2-relation-trait": "*"

```

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

Usage At Model
--------------

[](#usage-at-model)

```
class MyModel extends ActiveRecord{
    use \mootensai\relation\RelationTrait;
}
```

Array Input &amp; Usage At Controller
-------------------------------------

[](#array-input--usage-at-controller)

It takes a normal array of POST. This is the example

```
// sample at controller
//$_POST['ParentClass'] = ['attr1' => 'value1','attr2' => 'value2'];
//$_POST['RelatedClass'][0] = ['attr1' => 'value1','attr2' => 'value2'];
if($model->loadAll(Yii:$app->request->post()) && $model->saveAll()){
    return $this->redirect(['view', 'id' => $model->id, 'created' => $model->created]);
}
```

\#Features

Array Output
------------

[](#array-output)

```
// I use this to send model & related through JSON / Serialize
print_r($model->getAttributesWithRelatedAsPost());
```

```
Array
(
    [MainClass] => Array
        (
            [attr1] => value1
            [attr2] => value2
        )

    [RelatedClass] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )

)

```

```
print_r($model->getAttributesWithRelated());
```

```
Array
(
    [attr1] => value1
    [attr2] => value2
    [relationName] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )
)

```

Use Transaction
---------------

[](#use-transaction)

So your data will be atomic (see : )

Use Normal Save
---------------

[](#use-normal-save)

So your behaviors still works

Add Validation At Main Model
----------------------------

[](#add-validation-at-main-model)

```
$form->errorSummary($model);
```

will give you

```
 # :
My Related Model #1 : Attribute is required

```

It Works On Auto Incremental PK Or Not (I Have Tried Use UUID)
--------------------------------------------------------------

[](#it-works-on-auto-incremental-pk-or-not-i-have-tried-use-uuid)

See here if you want to use my behavior :

\#To Do Test it on another DB. I only test it on MySQL.

I'm open for any improvement

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 85% 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 ~54 days

Recently: every ~139 days

Total

18

Last Release

3062d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d35981376002cdf5be0414c02aec82b5fa59aaad13531658ece24e55b38bb92e?d=identicon)[moccia.f](/maintainers/moccia.f)

---

Top Contributors

[![mootensai](https://avatars.githubusercontent.com/u/5844149?v=4)](https://github.com/mootensai "mootensai (91 commits)")[![stesifrancesco](https://avatars.githubusercontent.com/u/17119418?v=4)](https://github.com/stesifrancesco "stesifrancesco (11 commits)")[![cgernert](https://avatars.githubusercontent.com/u/17023559?v=4)](https://github.com/cgernert "cgernert (2 commits)")[![dibyanshu11](https://avatars.githubusercontent.com/u/11568078?v=4)](https://github.com/dibyanshu11 "dibyanshu11 (2 commits)")[![bitdeli-chef](https://avatars.githubusercontent.com/u/3092978?v=4)](https://github.com/bitdeli-chef "bitdeli-chef (1 commits)")

---

Tags

yii2saveloadrelationtransactionrelatedloadwithrelationsavewithrelationsaveallloadall

### Embed Badge

![Health badge](/badges/stesi-yii2-relation-trait/health.svg)

```
[![Health](https://phpackages.com/badges/stesi-yii2-relation-trait/health.svg)](https://phpackages.com/packages/stesi-yii2-relation-trait)
```

###  Alternatives

[mootensai/yii2-relation-trait

Yii 2 Models load with relation, &amp; transaction save with relation

47220.3k9](/packages/mootensai-yii2-relation-trait)[yii2tech/illuminate

Yii2 to Laravel Migration Package

11315.1k](/packages/yii2tech-illuminate)[dmstr/yii2-db

Database extensions

19618.8k6](/packages/dmstr-yii2-db)[spanjeta/yii2-backup

Database Backup and Restore functionality

285.0k1](/packages/spanjeta-yii2-backup)

PHPackages © 2026

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