taxonomy term implementation in Yii2
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist mhndev/yii2-taxonomy-term "1.*"
or add
"mhndev/yii2-taxonomy-term": "1.*"
to the require section of your composer.json
file.
php yii migrate --migrationPath=@vendor/mhndev/yii2-taxonomy-term/src/migrations
user mhndev\yii2TaxonomyTerm\traits\TermableTrait in each Model which you want to use taxonomy-term for
class Post extends ActiveRecord
{
use TermableTrait;
/**
* @return string
*/
public static function tableName()
{
return 'posts';
}
/**
* @return array
*/
public function rules()
{
return [
[['title'], 'required'],
[['text'], 'required'],
];
}
}
$term = Term::findOne(['id'=>1]);
$post = Post::findOne(['id'=>1]);
$post->attachTerm($term);
$term = Term::findOne(['id'=>1]);
$post = Post::findOne(['id'=>1]);
$post->detachTerm($term);
$post = Post::findOne(['id'=>1]);
$post->listTerms();
$term = Term::findOne(['id'=>1]);
$term->getTree();