lubos / expandable
该包已被废弃,不再维护。未建议替代包。
最新版本(1.0.3)的包没有可用的许可证信息。
CakePHP 可扩展插件
1.0.3
2014-05-07 06:33 UTC
Requires
- php: >=5.2.8
- cakephp/cakephp: ~2.0
- composer/installers: ~1.0
This package is auto-updated.
Last update: 2023-08-04 17:09:48 UTC
README
允许在两个表中对每个条目保存不同的字段,而不接触模式。
它的工作方式类似于 Cake 的 i18n 表
安装
通过将 git clone 到您的插件文件夹或使用 composer。对于现有应用程序,您可以将以下内容添加到您的 composer.json
文件中
"require": {
"lubos/expandable": "~1.0"
}
并运行 php composer.phar update
它做什么
ExpandableBehavior 将允许您使用任何不存在于其模式中的“额外字段”来扩展任何模型。
它使用一个第二张表/模型作为键/值表,该表与主表/模型相关联。因此,您可以存储您想要的任何详细信息,与主表/模型分开,使模式更简单,并减少(主要)表的大小。
设置
git clone git://github.com/LubosRemplik/CakePHP-Expandable-Plugin.git app/Plugin/Expandable
您必须创建一个新的表来存储键/值,并可选地创建一个用于该表的模型。它应该命名为类似 'my_model_expands' 的东西,并需要以下字段
- 一个主ID
- 一个外键,链接回主表
- 一个 "key" 字段
- 一个 "value" 字段 ** 如果您想要返回 null,则根据您想要存储的内容是 varchar 还是 text
在您的模式中
public $my_model_expands = array(
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'my_model_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'index'),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'key' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 128, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'value' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'search' => array('column' => array('account_id', 'key'), 'unique' => 1)
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);
您可以通过 shell 创建该表
./cake schema create
n
y
在 MyModel 上
设置 Expandable 行为,使用 "with" 配置扩展模型,并确保您将扩展模型放入 "hasMany" 数组中。
public $actsAs = array(
'Expandable.Expandable' => array(
'with' => 'MyModelExpand',
)
);
public $hasMany = array('MyModelExpand');
用法
只需在模型上执行正常的 save()
,并执行正常的 find()
(包含扩展数据)。
您将看到不在模式中的额外字段...添加多少都可以。
有关此功能的更多信息以及功能的一个简单示例,请查看打包的单元测试
./cake test Expandable Model/Behavior/ExpandableBehavior
致谢
主要来源
- @link http://debuggable.com/posts/unlimited-model-fields-expandable-behavior:48428c2e-9a88-47ec-ae8e-77a64834cda3
- @link https://github.com/felixge/debuggable-scraps/blob/master/cakephp/behaviors/expandable/expandable.php
- @link https://github.com/felixge/debuggable-scraps/blob/master/cakephp/behaviors/expandable/expandable.test.php
重新打包
更新