PHPackages                             ivankff/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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. ivankff/yii2-value-objects

ActiveYii2-extension[Parsing &amp; Serialization](/categories/parsing)

ivankff/yii2-value-objects
==========================

Easy to store nested objects and typed collections in AR

0.1.3(5y ago)029BSD-3-ClausePHPPHP &gt;=5.6

Since Jan 9Pushed 5y ago2 watchersCompare

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

READMEChangelogDependencies (1)Versions (4)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 [
            'valueObjects' => 'ivankff\valueObjects\ValueObjectsBehavior',
        ];
    }

    /**
     * Value objects map
     *
     * @param static $owner
     * @return array
     */
    public static function valueObjects($owner) {
        // 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

```
