PHPackages                             inquid/yii2-relation-traits - 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. inquid/yii2-relation-traits

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

inquid/yii2-relation-traits
===========================

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

1.1.9(6y ago)0633BSD-3-ClausePHPPHP &gt;=5.4.0

Since Jun 3Pushed 5y agoCompare

[ Source](https://github.com/inquid/yii2-relation-traits)[ Packagist](https://packagist.org/packages/inquid/yii2-relation-traits)[ Docs](http://github.com/inquid/yii2-mongo-relation-trait)[ RSS](/packages/inquid-yii2-relation-traits/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (1)Versions (24)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())

PLUS soft delete/restore feature! For MongoDB!

Used in [inquid/yii2-enhanced-gii](https://github.com/inquid/yii2-enhanced-gii)

Support Mootnesai
-----------------

[](#support-mootnesai)

[![Support via Gratipay](https://camo.githubusercontent.com/1c5285a90da2f0b1ee32a7136cacdc9230404760abc467b90492670975ad5bd6/68747470733a2f2f63646e2e7261776769742e636f6d2f67726174697061792f67726174697061792d62616467652f322e332e302f646973742f67726174697061792e737667)](https://gratipay.com/mootensai/)

Endorse me on LinkedIn

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

```
Array (
    $_POST['ParentClass'] => Array
        (
            [attr1] => value1
            [attr2] => value2
            // has many
            [relationName] => Array
                (
                    [0] => Array
                        (
                            [relAttr] => relValue1
                        )
                    [1] => Array
                        (
                            [relAttr] => relValue1
                        )
                )
            // has one
            [relationName] => Array
                (
                    [relAttr1] => relValue1
                    [relAttr2] => relValue2
                )
        )
)

OR

Array (
    $_POST['ParentClass'] => ['attr1' => 'value1','attr2' => 'value2'],
    // Has One
    $_POST['RelatedClass'] => ['relAttr1' => 'value1','relAttr2' => 'value2'],
    // Has Many
    $_POST['RelatedClass'] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
            [1] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )
)
```

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

Features
========

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

Soft Delete
-----------

[](#soft-delete)

Add this line to your Model to enable soft delete

```
private $_rt_softdelete;

function __construct(){
    $this->_rt_softdelete = [
        '' =>
        // multiple row marker column example
        'isdeleted' => 1,
        'deleted_by' => \Yii::$app->user->id,
        'deleted_at' => date('Y-m-d H:i:s')
    ];
}
```

Add this line to your Model to enable soft restore

```
private $_rt_softrestore;

function __construct(){
    $this->_rt_softrestore = [
        '' =>
        // multiple row marker column example
        'isdeleted' => 0,
        'deleted_by' => 0,
        'deleted_at' => 'NULL'
    ];
}
```

### Should work on Yii's supported DB

[](#should-work-on-yiis-supported-db)

It use all Yii's Active Query or Active Record to execute DB command

### I'm open for any improvement

[](#im-open-for-any-improvement)

Please create issue if you got a problem or an idea for enhancement

#### ~ SDG ~

[](#-sdg-)

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 83.1% 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 ~87 days

Recently: every ~223 days

Total

20

Last Release

2331d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1505641?v=4)[Luis Gonzalez](/maintainers/gogl92)[@gogl92](https://github.com/gogl92)

---

Top Contributors

[![mootensai](https://avatars.githubusercontent.com/u/5844149?v=4)](https://github.com/mootensai "mootensai (118 commits)")[![gogl92](https://avatars.githubusercontent.com/u/1505641?v=4)](https://github.com/gogl92 "gogl92 (14 commits)")[![markux](https://avatars.githubusercontent.com/u/373611?v=4)](https://github.com/markux "markux (2 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)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![cronfy](https://avatars.githubusercontent.com/u/762954?v=4)](https://github.com/cronfy "cronfy (1 commits)")[![mzglinski](https://avatars.githubusercontent.com/u/23341314?v=4)](https://github.com/mzglinski "mzglinski (1 commits)")[![bitdeli-chef](https://avatars.githubusercontent.com/u/3092978?v=4)](https://github.com/bitdeli-chef "bitdeli-chef (1 commits)")

---

Tags

mongodbyii2mongosaveloadrelationtransactionrelatedloadwithrelationsavewithrelationsaveallloadall

### Embed Badge

![Health badge](/badges/inquid-yii2-relation-traits/health.svg)

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

###  Alternatives

[mootensai/yii2-relation-trait

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

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

PHPackages © 2026

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