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

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

deadmantfa/yii2-relation-trait
==============================

Yii 2 Models load with relation &amp; transaction save with relation (plus optional soft-delete/restore).

2.1.0(1mo ago)088BSD-3-ClausePHPPHP &gt;=7.4.0

Since Jun 3Pushed 1mo ago1 watchersCompare

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

READMEChangelog (2)Dependencies (4)Versions (33)Used By (0)

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

[](#yii2-relation-trait)

> **Note**: This is **not** the official extension by [@mootensai](https://github.com/mootensai).
> I am not the creator of the original extension. I have made bug fixes and improvements that suit my use case.
> Feel free to use it or refer to the official package at [mootensai/yii2-relation-trait](https://github.com/mootensai/yii2-relation-trait).

Yii 2 Models add functionality for loading related models via `loadAll($POST)` and transactional saving via `saveAll()`.
It also supports **soft delete** and **soft restore** features.

Works best with [mootensai/yii2-enhanced-gii](https://github.com/mootensai/yii2-enhanced-gii).

Badges
------

[](#badges)

[![Latest Stable Version](https://camo.githubusercontent.com/45c22c9157f74fbecf6a5ac9ece236516e8f80a31996b30e57f59f7f78159b0d/68747470733a2f2f706f7365722e707567782e6f72672f646561646d616e7466612f796969322d72656c6174696f6e2d74726169742f762f737461626c65)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)[![License](https://camo.githubusercontent.com/f322bf33a39cf003b53e5ece533babd3cb34e9de8e53e1976fb516672402ad33/68747470733a2f2f706f7365722e707567782e6f72672f646561646d616e7466612f796969322d72656c6174696f6e2d74726169742f6c6963656e7365)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)[![Total Downloads](https://camo.githubusercontent.com/70f9a0022d80ab0b4c48f1c96f3163de11aebeb65b8e6ff38a2766033a0d3fbd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646561646d616e7466612f796969322d72656c6174696f6e2d74726169742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)[![Monthly Downloads](https://camo.githubusercontent.com/0d46b08007bf0fd72960f933b965198d44d7c2506249118122f63cecc114923f/68747470733a2f2f706f7365722e707567782e6f72672f646561646d616e7466612f796969322d72656c6174696f6e2d74726169742f642f6d6f6e74686c79)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)[![Daily Downloads](https://camo.githubusercontent.com/71f7189a25d11a4c2b6f7ccca4d3882d8b049a53640783a272ba6e0aa99fbf11/68747470733a2f2f706f7365722e707567782e6f72672f646561646d616e7466612f796969322d72656c6174696f6e2d74726169742f642f6461696c79)](https://packagist.org/packages/deadmantfa/yii2-relation-trait)

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

[](#installation)

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

Either run

```
composer require deadmantfa/yii2-relation-trait
```

or add

```
"deadmantfa/yii2-relation-trait": "^2.0.0"
```

to the require section of your application's `composer.json` file.

Usage in the Model
------------------

[](#usage-in-the-model)

```
use deadmantfa\relation\RelationTrait;

class MyModel extends \yii\db\ActiveRecord
{
    use RelationTrait;

    // ...
}
```

Controller Usage
----------------

[](#controller-usage)

The extension expects a **normal array of POST** data. For example:

```
[
    $_POST['ParentClass'] => [
        'attr1' => 'value1',
        'attr2' => 'value2',
        // Has many
        'relationName' => [
            [ 'relAttr' => 'relValue1' ],
            [ 'relAttr' => 'relValue2' ]
        ],
        // Has one
        'relationName' => [
            'relAttr1' => 'relValue1',
            'relAttr2' => 'relValue2'
        ]
    ]
];
```

In your controller:

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

Features

1. Transaction Support a. Your data changes are atomic (ACID compliant).
2. Normal `save()`a. Behaviors still work as usual since it’s built on top of Yii’s `ActiveRecord`.
3. Validation a. Errors from related models appear via `errorSummary()`, e.g. ```
        MyRelatedClass #2: [Error message]

    ```
4. UUID or Auto-Increment Works with any PK strategy, including [mootensai/yii2-uuid-behavior](https://github.com/mootensai/yii2-uuid-behavior).
5. Soft Delete By defining `$_rt_softdelete` in your model constructor (and `$_rt_softrestore` for restoring), you can softly mark rows as deleted instead of physically removing them. ```
         private $_rt_softdelete;
         private $_rt_softrestore;

         public function __construct($config = [])
         {
             parent::__construct($config);

             $this->_rt_softdelete = [
                 'is_deleted' => 1,
                 'deleted_by' => Yii::$app->user->id,
                 'deleted_at' => date('Y-m-d H:i:s'),
             ];

             $this->_rt_softrestore = [
                 'is_deleted' => 0,
                 'deleted_by' => null,
                 'deleted_at' => null,
             ];
         }
    ```

Array Outputs
-------------

[](#array-outputs)

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

Produces a POST-like structure with the main model and related arrays.

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

Produces a nested structure under `[relationName] => [...]`.

Contributing or Reporting Issues
--------------------------------

[](#contributing-or-reporting-issues)

Please open an [issue](https://github.com/deadmantfa/yii2-relation-trait/pulls) or submit a PR if you find a bug or have an improvement idea.

---

### Disclaimer

[](#disclaimer)

This package is a **fork** or an alternative to [mootensai/yii2-relation-trait](https://github.com/mootensai/yii2-relation-trait). **All credit** to [@mootensai](https://github.com/mootensai) for the initial code. **This is not meant to replace** the original package but rather provide bug fixes and enhancements under a different namespace.

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance88

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 78% 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 ~131 days

Recently: every ~739 days

Total

31

Last Release

58d ago

Major Versions

1.1.15 → 2.0.02025-01-06

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

2.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/273b16723bec678f1ab9863eb3de2b31f2aa2390b5d872e547fda0dcfc427e61?d=identicon)[deadmantfa](/maintainers/deadmantfa)

---

Top Contributors

[![mootensai](https://avatars.githubusercontent.com/u/5844149?v=4)](https://github.com/mootensai "mootensai (117 commits)")[![deadmantfa](https://avatars.githubusercontent.com/u/1812611?v=4)](https://github.com/deadmantfa "deadmantfa (23 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

yii2saveloadrelationtransactionrelatedloadwithrelationsavewithrelationsaveallloadall

###  Code Quality

Static AnalysisRector

### Embed Badge

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

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