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

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

marena/yii2-relation-trait
==========================

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

1.1.10(11mo ago)03.4k↓33.3%2BSD-3-ClausePHPPHP &gt;=7.0.0

Since Jun 3Pushed 11mo agoCompare

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

READMEChangelog (9)Dependencies (1)Versions (22)Used By (2)

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)[![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)

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

[](#installation)

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

Either run

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

or add

```
"mootensai/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

40

—

FairBetter than 88% of packages

Maintenance51

Moderate activity, may be stable

Popularity19

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 86.9% 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 ~183 days

Recently: every ~209 days

Total

21

Last Release

344d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.4.0

1.1.6PHP &gt;=7.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/d38d3a0193a5134237a11b8522d1602194db34e7f2f9c12c1efca2ac9876c069?d=identicon)[marena](/maintainers/marena)

---

Top Contributors

[![mootensai](https://avatars.githubusercontent.com/u/5844149?v=4)](https://github.com/mootensai "mootensai (73 commits)")[![marena](https://avatars.githubusercontent.com/u/583615?v=4)](https://github.com/marena "marena (11 commits)")

---

Tags

yii2saveloadrelationtransactionrelatedloadwithrelationsavewithrelationsaveallloadall

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/marena-yii2-relation-trait/health.svg)](https://phpackages.com/packages/marena-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)[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)
