rangelreale/yii2-mdhaliasbehavior

Yii behavior for model attribute aliasing and format conversion using PHP MDH

dev-master 2018-04-19 17:05 UTC

This package is not auto-updated.

Last update: 2024-11-18 14:58:12 UTC


README

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist RangelReale/yii2-mdhaliasbehavior "*"

or add

"RangelReale/yii2-mdhaliasbehavior": "*"

to the require section of your composer.json.

The idea

This behavior adds an attribute alias that can perform format conversion between 2 data formats.

The aliased field by default is named _alias.

How to use

In your model:

/**
 * @property string posted_at This is your property that you have in the database table, it has DATETIME format
 */
class Post extends ActiveRecord
{
    // ... Some code here

    public function behaviors()
    {
        return [
            'alias' => [
                'class' => AliasBehavior::className(), // Our behavior
                'attributes' => [
                    'start_date' => [
                        'dataType' => 'datetime',
                    ],
                ],
            ]
        ];
    }
}

How is works

Behavior creates "virtual" attribute named attribute_name_alias for each attribute you define in the 'attributes' section. When you read $yourModel->attribute_name_alias behavior will return object with the type AliasAttribute. If this object will be used in the string context, it will be converted to string with the magical __toString method. And during this original value of attribute_name will be converted into the local representation.

When you assign value to the $yourModel->attribute_name_alias internally it will be assigned to value property of the AliasAttribute class and converted to the your database-stored property.

You can also define individual configuration for each attribute and define it's local name, format and so on.

Credits

Based on omnilight/yii2-datetime.