PHPackages                             equicolor/yii2-value-objects - 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. equicolor/yii2-value-objects

ActiveYii2-extension

equicolor/yii2-value-objects
============================

Easy to store nested objects and typed collections in AR

2511[1 issues](https://github.com/equicolor/yii2-value-objects/issues)PHP

Since Nov 30Pushed 8y ago1 watchersCompare

[ Source](https://github.com/equicolor/yii2-value-objects)[ Packagist](https://packagist.org/packages/equicolor/yii2-value-objects)[ RSS](/packages/equicolor-yii2-value-objects/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Yii2 Value Objects behavior
===========================

[](#yii2-value-objects-behavior)

Extend your ActiveRecord by nested objects and typed collections, serialized and stored in table field

Example

```
class User extends ActiveRecord
{
    // using value objects require attached behavior
    public function behaviors()
    {
        return [
            ValueObjectsBehavior::className(),
        ];
    }

    /**
     * Value objects map
     *
     * @return array
     */
    public static function valueObjects() {
        // define value objects on model attributes
        return [
            // $this->profile attribute will be an instance of defined anonymous class
            'profile' => new class extends ValueObject {
                public $github;
                public $phones = [];
            },
        ];
    }
}

$user = new User();
$user->profile->github = 'https://github.com/equicolor/';
$user->profile->phones[] = '555-55-555';
$user->save();
```

Now `profile` field of `user` table contains json:

```
{"github":"https://github.com/equicolor/","phones":["555-55-555"]}
```

It will be converted to object on afterFind event.

A more complex example with collections

```
