PHPackages                             indigerd/yii2-embedded-models - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. indigerd/yii2-embedded-models

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

indigerd/yii2-embedded-models
=============================

Behaviors for hydration and extraction of embedded models for yii2 framework

v0.0.5(7y ago)03.8k1MITPHPPHP &gt;=7.2.0

Since Jun 4Pushed 7y agoCompare

[ Source](https://github.com/Indigerd/yii2-embedded-models)[ Packagist](https://packagist.org/packages/indigerd/yii2-embedded-models)[ RSS](/packages/indigerd-yii2-embedded-models/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

Yii2 Embedded Models
====================

[](#yii2-embedded-models)

Usage
-----

[](#usage)

In order to embedd another models to your model using you should extend it from one provided by library. Use HydrateBehavior to embed single model to property or HydrateCollectionBehavior to embed collection of models. To correctly attach behavior you have to provide valid configuration values to following properties:

- `attribute` name of attribute in primary model in which you want to embed
- `targetModel` class name of embedded model
- `hydrator` Hydrator which will be used to hydrate and extract. You can provide class name if you configured hydrator in your DI container, array style definition for Yii::createObject or instance of Hydrator

Your embedded models can also embed another models. When validate will be called for your primary model it will be called for all your embedded models and fields in your primary model coreesponding to embedded models will be correctly populated with error messages. Embedded models will be automatically created by behavior when yor model is populated from database or when it is populated with `Model::load()` data from request. To auto populate field that is embedded model with `Model::load()` you need to add this field to `rules()` as `safe`

Example of attaching behavior to mongodb ActiveRecord model

```
# configure default Hydrator object in your DI

Yii::$container->set(
    'Indigerd\Hydrator\Accessor\AccessorInterface',
    'Indigerd\Hydrator\Accessor\PropertyAccessor'
);

Yii::$container->set(
    'Indigerd\Hydrator\Hydrator'
);

# Primary ("parent") model

use Indigerd\Hydrator\Hydrator;
use indigerd\embedded\behavior\HydrateBehavior;
use indigerd\embedded\behavior\HydrateCollectionBehavior;
use indigerd\embedded\model\mongodb\ActiveRecord;

class Clinic extends ActiveRecord
{
    public static function collectionName(): string
    {
        return 'clinics';
    }

    public function attributes(): array
    {
        return [
            '_id',
            'name',
            'country',
            'doctors',
        ];
    }

    public function behaviors() : array
    {
        return [
            [
                'class' => HydrateBehavior::class,
                'hydrator' => Hydrator::class,
                'targetModel' => Country::class,
                'attribute' => 'country'
            ],
            [
                'class' => HydrateCollectionBehavior::class,
                'hydrator' => Hydrator::class,
                'targetModel' => Doctor::class,
                'attribute' => 'doctors'
            ],
        ];
    }

}

# Country model

use yii\base\Model;

class Country extends Model
{
    protected $name;

    protected $code;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setCode($code)
    {
        $this->code = $code;
    }

    public function getCode()
    {
        return $this->code;
    }

    public function fields() : array
    {
        return [
            'name',
            'code'
        ];
    }
}

# Doctor model

use indigerd\embedded\model\Model;

class Doctor extends Model
{
    protected $name;

    protected $contact;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setContact(Contact $contact)
    {
        $this->contact = $contact;
    }

    public function getContact()
    {
        return $this->contact;
    }

    public function fields() : array
    {
        return [
            'name',
            'contact'
        ];
    }

    public function behaviors() : array
    {
        return [
            [
                'class' => HydrateBehavior::class,
                'hydrator' => Hydrator::class,
                'targetModel' => Contact::class,
                'attribute' => 'contact'
            ],
        ];
    }
}

# Contact model

use yii\base\Model;

class Contact extends Model
{
    protected $phone;

    protected $email;

    public function setPhone($phone)
    {
        $this->phone = $phone;
    }

    public function getPhone()
    {
        return $this->phone;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function fields() : array
    {
        return [
            'phone',
            'email'
        ];
    }
}
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

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 ~5 days

Total

5

Last Release

2565d ago

### Community

Maintainers

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

---

Top Contributors

[![Indigerd](https://avatars.githubusercontent.com/u/2143512?v=4)](https://github.com/Indigerd "Indigerd (6 commits)")

---

Tags

activerecordembedembeddedembedded-modelsembedded-mongodbmongodbnestedyii2libraryextractionhydrationembedded model

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/indigerd-yii2-embedded-models/health.svg)

```
[![Health](https://phpackages.com/badges/indigerd-yii2-embedded-models/health.svg)](https://phpackages.com/packages/indigerd-yii2-embedded-models)
```

###  Alternatives

[fedemotta/yii2-widget-datatables

DataTables widget for Yii2

36184.4k1](/packages/fedemotta-yii2-widget-datatables)[prawee/yii2-vuejs

Vue.js library for Yii2

1712.3k](/packages/prawee-yii2-vuejs)

PHPackages © 2026

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