项目作者: mhndev

项目描述 :
yii2 implementation of taxonomy-term
高级语言: PHP
项目地址: git://github.com/mhndev/yii2-taxonomy-term.git
创建时间: 2016-08-29T07:56:19Z
项目社区:https://github.com/mhndev/yii2-taxonomy-term

开源协议:

下载


Taxonomy Term

taxonomy term implementation in Yii2

Installation

The preferred way to install this extension is through composer.

Either run

  1. composer require --prefer-dist mhndev/yii2-taxonomy-term "1.*"

or add

  1. "mhndev/yii2-taxonomy-term": "1.*"

to the require section of your composer.json file.

Execute Migrations

  1. php yii migrate --migrationPath=@vendor/mhndev/yii2-taxonomy-term/src/migrations

Usage

user mhndev\yii2TaxonomyTerm\traits\TermableTrait in each Model which you want to use taxonomy-term for

example

Post Model
  1. class Post extends ActiveRecord
  2. {
  3. use TermableTrait;
  4. /**
  5. * @return string
  6. */
  7. public static function tableName()
  8. {
  9. return 'posts';
  10. }
  11. /**
  12. * @return array
  13. */
  14. public function rules()
  15. {
  16. return [
  17. [['title'], 'required'],
  18. [['text'], 'required'],
  19. ];
  20. }
  21. }

attach a term to an entity

  1. $term = Term::findOne(['id'=>1]);
  2. $post = Post::findOne(['id'=>1]);
  3. $post->attachTerm($term);

detach a term from an entity

  1. $term = Term::findOne(['id'=>1]);
  2. $post = Post::findOne(['id'=>1]);
  3. $post->detachTerm($term);

list terms of an entity

  1. $post = Post::findOne(['id'=>1]);
  2. $post->listTerms();

get term tree

  1. $term = Term::findOne(['id'=>1]);
  2. $term->getTree();