artkost / yii2-taxonomy
Yii 2 分类和标签模块
dev-develop
2017-03-20 20:28 UTC
Requires
- php: >=5.5
- paulzi/yii2-nested-sets: ^1.0
- yiisoft/yii2: ~2.0.5
Requires (Dev)
- phpunit/dbunit: ~1.0
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-08-29 04:42:49 UTC
README
此模块受Drupal分类系统启发,功能相同。
使用方法
配置
return [ 'components' => [ 'taxonomy' => [ 'class' => '\artkost\taxonomy\Module' ] ] ]
我们有以下表格
news_post
- id
- 标题
- 缩略名
- 内容
- 更新时间
- 创建时间
news_term
- post_id // news_post.id 新闻帖子ID
- vocab_id // taxonomy_term.vid 因为可以有多个类型的分类、标签等。
- term_id // taxonomy_term.id 术语ID
使用方法
将行为附加到模型
class NewsTerm extends ActiveRecord { public static function saveTerm($vocabID, $postID, $termID) { return (new self([ 'post_id' => $postID, 'vocab_id' => $vocabID, 'term_id' => $termID, ]))->save(); } public static function saveTerms($vocabID, $postID, array $termIDs) { foreach ($termIDs as $termID) { self::saveTerm($vocabID, $postID, $termID); } } public static function removeTerm($vocabID, $postID, $termID = false) { $condition = [ 'post_id' => $postID, 'vocab_id' => $vocabID ]; if ($termID) { $condition['term_id'] = $termID; TaxonomyTerm:: deleteAll(['id' => $termID]); } else { TaxonomyTerm:: deleteAll(['vid' => $vocabID]); } return self::deleteAll($condition); } } class NewsPost extends ActiveRecord { public function behaviors() { return [ 'termBehavior' => [ 'class' => artkost\taxonomy\behaviors\TermBehavior::className(), 'vocabularies' => [ 'category' => [ 'name' => 'news-category', //machine name of vocab, must be uniq for whole project 'create' => true // auto create if not exists ], 'tags' => [ 'name' => 'news-tags', 'create' => true ], ] ] ]; } public static function find() { return (new ActiveQuery(get_called_class())) ->with('tagsTerms', 'categoryTerm'); } /** * @return \artkost\taxonomy\models\TaxonomyVocabulary */ public function getCategory() { return $this->getVocabulary('category'); } /** * @return TaxonomyTerm */ public function getCategoryTerm() { $vocabID = $this->category->id; return $this->hasOne(TaxonomyTerm::className(), ['id' => 'term_id']) ->viaTable(NewsTerm::tableName(), ['post_id' => 'id'], function ($query) use ($vocabID) { return $query->andWhere(['vocab_id' => $vocabID]); }); } public function setCategoryTerm($termID) { $this->categoryID = (int) $termID; } /** * @return \artkost\taxonomy\models\TaxonomyVocabulary */ public function getTags() { return $this->getVocabulary('tags'); } /** * @return TaxonomyTerm[] */ public function getTagsTerms() { $vocabID = $this->tags->id; return $this->hasMany(TaxonomyTerm::className(), ['id' => 'term_id']) ->viaTable(NewsTerm::tableName(), ['post_id' => 'id'], function ($query) use ($vocabID) { return $query->andWhere(['vocab_id' => $vocabID]); }); } public function setTagsTerms(array $termsIDs) { $this->tagsIDs = $termIDs; } public function afterSave($insert, $changedAttributes) { // we can have only one category per news post if ($this->categoryID) { NewsTerm::removeTerm($this->category->id, $this->id); NewsTerm::saveTerm($this->category->id, $this->id, $this->categoryID); } if (!empty($this->tagsIDs)) { NewsTerm::removeTerm($this->tags->id, $this->id); //removes all NewsTerm::saveTerms($this->tags->id, $this->id, $this->tagsIDs); } } }
在视图或控制器中,您可以访问