transform-attributes-behavior Transform values of attributes before saving to DB and after reading from DB.

Behavior for Yii1.x CActiveRecord. Transform values of attributes before saving to DB and after reading from DB. Used the beforeSave/afterSave handlers, so the transform when recording does work only with the methods save(), update(), insert().

Requirements

Yii 1.1 or above

Usage

Functions for transformations

  1. Requirements
  2. Usage
  3. Resources

You can assign functions for both transformations, if the functions is not assigned, the default setting when recording in database attributes is converted to JSON (CJSON::encode), when reading the contrary (CJSON::decode).

You can overlap default functions and use e.g. serialize()/unserialize():

public function behaviors(){
        return array(
            'TransformAttributesBehavior' => array(
                'class' => 'application.components.TransformAttributesBehavior',
                // 'attribute1', 'attribute2' - attributes for transform
                'transformations' => array('attribute1', 'attribute2'),
                'callbackToDb' => function ($model, $attributeName) {
                        return is_string($model->$attributeName) ? $model->$attributeName : serialize($model->$attributeName);
                    },
                'callbackFromDb' => function ($model, $attributeName) {
                    return empty($model->$attributeName) ? $model->$attributeName : unserialize($model->$attributeName);
                }
            )
        );
    }

You can specify a separate transformation function for an attribute, see examples below.

Attributes for transformations

Attributes for transform defined:

1) In method behaviors() - property 'transformations'

public function behaviors(){
        return array(
            'TransformAttributesBehavior' => array(
                'class' => 'application.components.TransformAttributesBehavior',
                // 'attribute1', 'attribute2' - attributes for transform
                'transformations' => array('attribute1', 'attribute2')
            )
        );
    }

2) In method attributeTransformations()

public function attributeTransformations(){
        return array('attribute1', 'attribute2');
}

Also, in behaviors() and attributeTransformations() can specify a separate transformation function for an attribute:

// default functions for 'attribute1'
        array('attribute1',
              'attribute2' => array(
                    // function transform 'attribute2' to DB
                    'to' => function ($model, $attributeName) {
                        return is_string($model->$attributeName) ? $model->$attributeName : serialize($model->$attributeName);
                    },
                    // function transform 'attribute2' from DB
                    'from' => function ($model, $attributeName) {
                        return empty($model->$attributeName) ? $model->$attributeName : unserialize($model->$attributeName);
                    }
              )
              );

Resources

0 0
3 followers
186 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Database
Developed by: yan
Created on: May 19, 2014
Last updated: 9 years ago

Downloads

show all

Related Extensions