yiisoft-custom/yii2-model-attribute-convert
Composer 安装命令:
composer require yiisoft-custom/yii2-model-attribute-convert
包简介
Base on Yii2 BaseActiveRecord. Converts column from db format to model format while SELECT,oppositely way while INSERT or UPDATE
README 文档
README
Attribute value converted between db format and Yii2 model format
Require yiisoft/yii2
Installation
composer require yiisoft-custom/yii2-model-attribute-convert
Notice & Important
Case the attribute set an converter in cast,it would be converted to model format while trigger EVENT_AFTER_FIND,
and the model value strictly is already changed (e.g: db value format is json [1,2,3],with 'Array' converter , model format would bearray(1,2,3)). the result of these method calls would be effected
:
isAttributeChanged(),
getOldAttribute(),
getOldAttributes(),
getDirtyAttributes()
But don't worry,we gotta the suitable calls instead while any attribute of model casted with a converter,list of mapper:
isAttributeChangedWithConverter() -> isAttributeChanged()
getOldAttributeWithConverter() -> getOldAttribute()
getOldAttributesWithConverter() -> getOldAttributes()
getDirtyAttributesWithConverter() -> getDirtyAttributes()
Usage
<?php class Test extends ActiveRecord { public function behaviors() { $behaviors = parent::behaviors(); $behaviors['attribute_convert'] = [ 'class' => AttributeConverted::class, 'cast' => [ 'order_no' => 'Array', ] ]; return $behaviors; } } ?>
Just used like other behavior class. Property $cast is key-value(eg: 'order_no' => 'Array'). Key is the attribute needs to be converted,and value is the converter,class name or string maps a class name would be fine
统计信息
- 总下载量: 64
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-10