creocoder/yii2-taggable
Composer 安装命令:
composer require creocoder/yii2-taggable
包简介
The taggable behavior for the Yii framework
README 文档
README
A modern taggable behavior for the Yii framework.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require creocoder/yii2-taggable
or add
"creocoder/yii2-taggable": "~2.0"
to the require section of your composer.json file.
Migrations
Run the following command
$ yii migrate/create create_post_table
Open the /path/to/migrations/m_xxxxxx_xxxxxx_create_post_table.php file,
inside the up() method add the following
$this->createTable('{{%post}}', [ 'id' => Schema::TYPE_PK, 'title' => Schema::TYPE_STRING . ' NOT NULL', 'body' => Schema::TYPE_TEXT . ' NOT NULL', ]);
Run the following command
$ yii migrate/create create_tag_table
Open the /path/to/migrations/m_xxxxxx_xxxxxx_create_tag_table.php file,
inside the up() method add the following
$this->createTable('{{%tag}}', [ 'id' => Schema::TYPE_PK, 'name' => Schema::TYPE_STRING . ' NOT NULL', 'frequency' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0', ]);
Run the following command
$ yii migrate/create create_post_tag_assn_table
Open the /path/to/migrations/m_xxxxxx_xxxxxx_create_post_tag_assn_table.php file,
inside the up() method add the following
$this->createTable('{{%post_tag_assn}}', [ 'post_id' => Schema::TYPE_INTEGER . ' NOT NULL', 'tag_id' => Schema::TYPE_INTEGER . ' NOT NULL', ]); $this->addPrimaryKey('', '{{%post_tag_assn}}', ['post_id', 'tag_id']);
Configuring
Configure model as follows
use creocoder\taggable\TaggableBehavior; /** * ... * @property string $tagValues */ class Post extends \yii\db\ActiveRecord { public function behaviors() { return [ 'taggable' => [ 'class' => TaggableBehavior::className(), // 'tagValuesAsArray' => false, // 'tagRelation' => 'tags', // 'tagValueAttribute' => 'name', // 'tagFrequencyAttribute' => 'frequency', ], ]; } public function rules() { return [ //... ['tagValues', 'safe'], ]; } public function transactions() { return [ self::SCENARIO_DEFAULT => self::OP_ALL, ]; } public static function find() { return new PostQuery(get_called_class()); } public function getTags() { return $this->hasMany(Tag::className(), ['id' => 'tag_id']) ->viaTable('{{%post_tag_assn}}', ['post_id' => 'id']); } }
Model Tag can be generated using Gii.
Configure query class as follows
use creocoder\taggable\TaggableQueryBehavior; class PostQuery extends \yii\db\ActiveQuery { public function behaviors() { return [ TaggableQueryBehavior::className(), ]; } }
Usage
Setting tags to the entity
To set tags to the entity
$post = new Post(); // through string $post->tagValues = 'foo, bar, baz'; // through array $post->tagValues = ['foo', 'bar', 'baz'];
Adding tags to the entity
To add tags to the entity
$post = Post::findOne(1); // through string $post->addTagValues('bar, baz'); // through array $post->addTagValues(['bar', 'baz']);
Remove tags from the entity
To remove tags from the entity
$post = Post::findOne(1); // through string $post->removeTagValues('bar, baz'); // through array $post->removeTagValues(['bar', 'baz']);
Remove all tags from the entity
To remove all tags from the entity
$post = Post::findOne(1); $post->removeAllTagValues();
Getting tags from the entity
To get tags from the entity
$posts = Post::find()->with('tags')->all(); foreach ($posts as $post) { // as string $tagValues = $post->tagValues; // as array $tagValues = $post->getTagValues(true); }
Return type of getTagValues can also be configured globally via tagValuesAsArray property.
Checking for tags in the entity
To check for tags in the entity
$post = Post::findOne(1); // through string $result = $post->hasTagValues('foo, bar'); // through array $result = $post->hasTagValues(['foo', 'bar']);
Search entities by any tags
To search entities by any tags
// through string $posts = Post::find()->anyTagValues('foo, bar')->all(); // through array $posts = Post::find()->anyTagValues(['foo', 'bar'])->all();
To search entities by any tags using custom tag model attribute
// through string $posts = Post::find()->anyTagValues('foo-slug, bar-slug', 'slug')->all(); // through array $posts = Post::find()->anyTagValues(['foo-slug', 'bar-slug'], 'slug')->all();
Search entities by all tags
To search entities by all tags
// through string $posts = Post::find()->allTagValues('foo, bar')->all(); // through array $posts = Post::find()->allTagValues(['foo', 'bar'])->all();
To search entities by all tags using custom tag model attribute
// through string $posts = Post::find()->allTagValues('foo-slug, bar-slug', 'slug')->all(); // through array $posts = Post::find()->allTagValues(['foo-slug', 'bar-slug'], 'slug')->all();
Search entities related by tags
To search entities related by tags
// through string $posts = Post::find()->relatedByTagValues('foo, bar')->all(); // through array $posts = Post::find()->relatedByTagValues(['foo', 'bar'])->all();
To search entities related by tags using custom tag model attribute
// through string $posts = Post::find()->relatedByTagValues('foo-slug, bar-slug', 'slug')->all(); // through array $posts = Post::find()->relatedByTagValues(['foo-slug', 'bar-slug'], 'slug')->all();
Donating
Support this project and others by creocoder via gratipay.
creocoder/yii2-taggable 适用场景与选型建议
creocoder/yii2-taggable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 134.37k 次下载、GitHub Stars 达 133, 最近一次更新时间为 2015 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tags」 「Taggable」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 creocoder/yii2-taggable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 creocoder/yii2-taggable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 creocoder/yii2-taggable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A powerfull cacher based on laravels file cache driver
A custom URL rule class for Yii 2 which allows to create translated URL rules
Doctrine2 taggable behavior extension
The PSR-16 extension to Apix-Cache.
Phalcon PHP Meta tags service
A tool for scraping URL resources (oEmbed, OpenGraph, Twitter cards, JSON-LD)
统计信息
- 总下载量: 134.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 138
- 点击次数: 33
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-01-06