wowkaster / yii2-serialize-attributes
该行为允许您将模型属性视为数组,只要您使用活动记录查找方法,它们就会在插入时自动序列化,在选中时自动反序列化。
dev-master
2015-03-01 10:46 UTC
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 16:37:40 UTC
README
安装
require : {
"wowkaster/yii2-serialize-attributes": "dev-master"
}
如何使用。
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')