PHP code example of ivankff / yii2-value-objects

1. Go to this page and download the library: Download ivankff/yii2-value-objects library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ivankff / yii2-value-objects example snippets


class User extends ActiveRecord
{
    // using value objects        '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();



use ivankff\valueObjects\ValueObject;
use yii\db\ActiveRecord; 

/**
 * @property integer $id
 * @property Offer $offer
 */
class Campaign extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'valueObjects' => 'ivankff\valueObjects\ValueObjectsBehavior',
        ];
    }
    /**
     * Value objects map
     *
     * @param static $owner
     * @return array
     */
    public static function valueObjects($owner) {
        return [
            // you can define value object as simple class
            'offer' => new Offer(['arAttribute' => 'offer_column_in_database']),
        ];
    }

    // other methods ...
}