riddlemd / tagit
CakePHP3 轻量级标签行为插件。别担心,Tagit。
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.3.2 <4.0.0
Requires (Dev)
This package is not auto-updated.
Last update: 2017-06-28 11:33:11 UTC
README
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 软件包的推荐方法是
composer require "riddlemd/tagit:1.*"
配置
在您的数据库上运行以下 SQL 语句以设置必要的表
#!mysql
CREATE TABLE `tagit_tags` (
`id` int(10) UNSIGNED NOT NULL,
`name` varchar(255) NOT NULL,
`count` int(10) UNSIGNED NOT NULL DEFAULT '0',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL
);
CREATE TABLE `tagit_tag_associations` (
`id` int(10) UNSIGNED NOT NULL,
`tag_id` int(10) UNSIGNED NOT NULL,
`fk_id` int(10) UNSIGNED NOT NULL,
`fk_table` varchar(255) NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL
);
如何使用
通过 composer 安装
将
$this->addBehavior('Riddlemd/Tagit.Tagit');
添加到您想要启用 Tagit 的表的初始化方法中。要创建标签,请将
tags_string
属性写入从步骤 2 中的表中检索到的实体中。(示例$entity->tags_string = 'Tag1, Tag2,Tag3, Tag 4';
)要读取标签,请在访问之前调用查询的 formatResults 方法,并给它
$this->TABLE_NAME->behaviors()->get("Tagit")->getResultsFormatter()
,其中 TABLE_NAME 是您使用的表名。(示例$articles = $this->Articles->find()->formatResults($this->Articles->behaviors()->get("Tagit")->getResultsFormatter());
)如果您正在使用$this->Articles->get()
或类似方法,则可以将具有$this->TABLE_NAME->behaviors()->get("Tagit")->getResultsFormatter()
的变量分配给单个实体以使用(示例:$resultsFormatter = $this->Articles->behaviors()->get("Tagit")->getResultsFormatter(); $article = $resultsFormatter($this->Articles->get(1));
)
该插件有几个配置选项可以在 addBehavior 调用中修改,有关它们的列表,请参阅 Riddlemd/Tagit/src/Model/Behavior/TagitBehavior.php 的 $_defaultConfig。
config/plug.php 中也有几个配置选项,您可以在 config/app.php 中覆盖它们,不要编辑 plugin.php,因为它将在升级时被覆盖。