wowkaster/yii2-serialize-attributes

This behavior allows you to treat model attributes as arrays, and they are automatically serialized for insertion and unserialized upon select, as long as you use the active record find methods.

Installs: 5 100

Dependents: 2

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 2

Type:yii2-extension

dev-master 2015-03-01 10:46 UTC

This package is not auto-updated.

Last update: 2024-11-09 17:24:54 UTC


README

Install

require : {
  "wowkaster/yii2-serialize-attributes": "dev-master"
}

How to use.

class SomeModel extends \yii\db\ActiveRecord {
  public functuin rules() {
    return [
      [['options'], 'safe']
    ];
  }
  
  ....
  
  public function behaviors() {
    return [
        [
            'class' => SerializeAttributesBehavior::className(),
            'convertAttr' => ['options' => 'json']
        ]
    ];
  }
  
  ====// OR //====
  public function behaviors() {
    return [
        [
            'class' => SerializeAttributesBehavior::className(),
            'convertAttr' => ['options' => 'serialize']
        ]
    ];
  }
}


$model = new SomeModel();
$model->options = ['value1', 'value2', 'param' => 'value'];
$model->save();

print_r($model->options); => Array('value1', 'value2', 'param' => 'value')

$model = SomeModel::findOne(1);
print_r($model->options); => Array('value1', 'value2', 'param' => 'value')