2017 © Pedro Peláez
 

yii2-extension yii2-mdhaliasbehavior

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

image

rangelreale/yii2-mdhaliasbehavior

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

  • Thursday, April 19, 2018
  • by RangelReale
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

MDH Alias Behavior for Yii2

Installation

The preferred way to install this extension is through composer., (*1)

Either run, (*2)

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

or add, (*3)

"RangelReale/yii2-mdhaliasbehavior": "*"

to the require section of your composer.json., (*4)

The idea

This behavior adds an attribute alias that can perform format conversion between 2 data formats., (*5)

The aliased field by default is named _alias., (*6)

How to use

In your model:, (*7)

/**
 * @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., (*8)

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., (*9)

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

Credits

Based on omnilight/yii2-datetime., (*11)

The Versions

19/04 2018

dev-master

9999999-dev

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

  Sources   Download

BSD-3-Clause

The Requires

 

by Rangel Reale

yii2 model behavior format