lrnzfrr / custom-fields
CakePHP 的 CustomFields 插件
v0.0.1
2019-03-26 11:13 UTC
Requires
- cakephp/cakephp: ^3.5
Requires (Dev)
- phpunit/phpunit: ^5.7.14|^6.0
This package is auto-updated.
Last update: 2024-09-29 05:21:38 UTC
README
使用此插件,您可以轻松地将自定义字段添加到所有记录中!
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
composer require lrnzfrr/custom-fields
加载插件
bin/cake plugin load lrnzfrr/CustomFields
使用插件
首先,确保您的实体可以访问 custom_fields
启动插件的迁移
bin/cake migrations migrate -p lrnzfrr/CustomFields
在模型表中添加行为
$this->addBehavior('lrnzfrr/CustomFields.CustomFieldable', ['accessibleFields'=>['article_type','article_status'] // custom fields accessible ]);
在控制器中使用查找方法
$articles = $this->Articles->find('withCustomFields');
在视图中
echo $this->Form->hidden('custom_fields.0.name',['value'=>'article_type']); echo $this->Form->text('custom_fields.0.value',['value'=>'','label'=>'article_type','placeholder'=>'Article Type']); echo $this->Form->hidden('custom_fields.1.name',['value'=>'article_status']); echo $this->Form->text('custom_fields.1.value',['value'=>'','label'=>'article_status','placeholder'=>'Article Status']);
如果您愿意,您也可以使用 getCustomFieldsAssoc 方法在实体内部以关联键 => 值的方式获取自定义字段
在实体内部
use lrnzfrr\CustomFields\Model\Traits\CustomFieldTrait; class .... use CustomFieldTrait; protected $_virtual = ['custom_fields_assoc']; /* ** Create a virtual property with custom Fields */ protected function _getCustomFieldsAssoc() { return $this->getCustomFieldsAssoc(); }
就是这样!