PHP code example of chemezov / yii2-dynamic-fields

1. Go to this page and download the library: Download chemezov/yii2-dynamic-fields 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/ */

    

chemezov / yii2-dynamic-fields example snippets


use chemezov\yii2_dynamic_fields\DynamicFieldsBehavior;

class User extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'dynamicFields' => [
                'class' => DynamicFieldsBehavior::class,
                'fields' => ['my_custom_string_1', 'my_custom_string_2'],
            ],
        ];
    }
        
    public function rules() {
        return [
            [['my_custom_string_1', 'my_custom_string_2'], 'string', 'max' => 255],
        ];
    }
        
    public function attributeLabels()
    {
        return [
            'my_custom_string_1' => 'My string 1',
            'my_custom_string_2' => 'My string 2',
        ];
    }
}

$model = new User();
$value = $model->my_custom_string_1; // get value
$model->my_custom_string_1 = 'some value'; // set value
$model->save();

use chemezov\yii2_dynamic_fields\DynamicFieldsBehavior;
use yii\behaviors\AttributeTypecastBehavior;

class User extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'dynamicFields' => [
                'class' => DynamicFieldsBehavior::class,
                'fields' => ['my_boolean_attribute'],
            ],
            'typecast' => [
                'class' => AttributeTypecastBehavior::class,
                'attributeTypes' => [
                    'my_boolean_attribute' => AttributeTypecastBehavior::TYPE_BOOLEAN,
                ],
                'typecastAfterFind' => true,
            ],
        ];
    }
        
    public function rules() {
        return [
            [['my_boolean_attribute'], 'default', 'value' => null],
            [['my_boolean_attribute'], 'boolean'],
        ];
    }
        
    public function attributeLabels()
    {
        return [
            'my_boolean_attribute' => 'My boolean attribute',
        ];
    }
}

use chemezov\yii2_dynamic_fields\DynamicFieldsBehavior;
use yii\behaviors\AttributeTypecastBehavior;

/**
 * Class User
 *
 * @property string $address
 * @property boolean $is_client
 */
class User extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
        ...
            [$this->getAdditionalFieldsNamesString(), 'string', 'max' => 255],
            [$this->getAdditionalFieldsNamesBoolean(), 'default', 'value' => null],
            [$this->getAdditionalFieldsNamesBoolean(), 'boolean'],
        ];
    }
    
    public function behaviors()
    {
        return [
            'dynamicFields' => [
                'class' => DynamicFieldsBehavior::class,
                'fields' => $this->getAdditionalFieldsNames(),
            ],
            'typecast' => [
                'class' => AttributeTypecastBehavior::class,
                'attributeTypes' => [
                    'is_client' => AttributeTypecastBehavior::TYPE_BOOLEAN,
                ],
                'typecastAfterFind' => true,
            ],
        ];
    }
    
    public function getAdditionalFieldsNamesString()
    {
        return [
            'address',
        ];
    }

    public function getAdditionalFieldsNamesBoolean()
    {
        return [
            'is_client',
        ];
    }

    public function getAdditionalFieldsNames()
    {
        return array_merge($this->getAdditionalFieldsNamesString(), $this->getAdditionalFieldsNamesBoolean());
    }

    public function fields()
    {
        return array_merge(parent::fields(), $this->getAdditionalFieldsNames());
    }
}

$this->addColumn('your_table', 'additional_data', $this->text()->null());

use chemezov\yii2_dynamic_fields\JsonDynamicFieldsBehavior;
use yii\behaviors\AttributeTypecastBehavior;

class User extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'dynamicFields' => [
                'class' => JsonDynamicFieldsBehavior::class,
                'fields' => ['my_boolean_attribute'],
            ],
            'typecast' => [
                'class' => AttributeTypecastBehavior::class,
                'attributeTypes' => [
                    'my_boolean_attribute' => AttributeTypecastBehavior::TYPE_BOOLEAN,
                ],
                'typecastAfterFind' => true,
            ],
        ];
    }
        
    public function rules() {
        return [
            [['my_boolean_attribute'], 'default', 'value' => null],
            [['my_boolean_attribute'], 'boolean'],
        ];
    }
        
    public function attributeLabels()
    {
        return [
            'my_boolean_attribute' => 'My boolean attribute',
        ];
    }
}

php composer.phar