cakebake / yii2-scalable-behavior
此包已被废弃,不再维护。未建议替代包。
键值存储是一个非常简单但非常强大的模型。使用此行为来扩展您的 Yii 2 模型而无需更改结构。
1.0.0-beta
2014-09-08 22:51 UTC
This package is not auto-updated.
Last update: 2020-01-20 03:53:02 UTC
README
键值存储是一个非常简单但非常强大的模型。使用此行为来扩展您的 Yii 2 模型而无需更改结构。
数据可以通过“虚拟属性”进行查询和保存。这些数据以序列化的形式存储在配置的表列中。
安装
安装此扩展的首选方法是使用 composer。
运行以下命令之一:
php composer.phar require --prefer-dist cakebake/yii2-scalable-behavior "*"
或
"cakebake/yii2-scalable-behavior": "*"
将以下内容添加到您的 composer.json
文件的 require 部分。
准备
在您的目标表中创建一个列。建议使用“文本”或“长文本”类型以保存尽可能多的数据。
使用方法
扩展安装完成后,只需在模型中添加以下内容即可使用它:
use cakebake\behaviors\ScalableBehavior; public function behaviors() { return [ ... 'scaleable' => [ 'class' => ScalableBehavior::className(), 'scalableAttribute' => 'value', // The owner object's attribute / the column of the corresponding table, which are used as storage for the virtual attributes 'virtualAttributes' => ['about_me', 'birthday'] // Definition of virtual attributes that are added to the owner object ], ... ]; }
现在我们可以像处理正常虚拟属性一样继续处理。
public function rules() { return [ ['about_me', 'required'], ['about_me', 'string'], ['birthday', 'string', 'max' => 60], ]; }
建议
此技术应仅用于元数据。不正确使用可能会对应用程序性能产生负面影响。