PHPackages                             artkost/yii2-taxonomy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. artkost/yii2-taxonomy

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

artkost/yii2-taxonomy
=====================

Yii 2 Taxonomy categories and tags module

10793[1 issues](https://github.com/artkost/yii2-taxonomy/issues)PHP

Since Jul 9Pushed 9y ago3 watchersCompare

[ Source](https://github.com/artkost/yii2-taxonomy)[ Packagist](https://packagist.org/packages/artkost/yii2-taxonomy)[ RSS](/packages/artkost-yii2-taxonomy/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Taxonomy module for Yii2
========================

[](#taxonomy-module-for-yii2)

This module inspired by Drupal taxonomy system and works same.

Usage
-----

[](#usage)

### Configuration

[](#configuration)

```
return [
  'components' => [
    'taxonomy' => [
      'class' => '\artkost\taxonomy\Module'
    ]
  ]
]
```

We have tables

`news_post`

- id
- title
- slug
- content
- updated\_at
- created\_at

`news_term`

- post\_id // news\_post.id news post id
- vocab\_id // taxonomy\_term.vid because we can have many types of categories, tags etc.
- term\_id // taxonomy\_term.id term id

### Usage

[](#usage-1)

Attach behavior to Model

```
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);
    }
  }
}
```

In view or controller you can access

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/441cca4d09df63db37a2aaa99f6481d67b62f0cb208031f3b38fbc8e2c7ff66b?d=identicon)[JiLiZART](/maintainers/JiLiZART)

---

Top Contributors

[![JiLiZART](https://avatars.githubusercontent.com/u/62051?v=4)](https://github.com/JiLiZART "JiLiZART (12 commits)")

### Embed Badge

![Health badge](/badges/artkost-yii2-taxonomy/health.svg)

```
[![Health](https://phpackages.com/badges/artkost-yii2-taxonomy/health.svg)](https://phpackages.com/packages/artkost-yii2-taxonomy)
```

###  Alternatives

[florianwolters/component-util-singleton

The Singleton (and Registry of Singletons a.k.a. Multiton) design pattern as a PHP component.

13175.8k5](/packages/florianwolters-component-util-singleton)[laravel-frontend-presets/white-dashboard

Laravel 11.x Front-end preset for white dashboard

507.8k](/packages/laravel-frontend-presets-white-dashboard)[moderntribe/tribe-libs

A library for use on Modern Tribe service projects.

1348.8k2](/packages/moderntribe-tribe-libs)[stechstudio/laravel-record

What if Laravel's Collection and Model classes had a baby?

2313.7k2](/packages/stechstudio-laravel-record)[paysera/lib-php-cs-fixer-config

PHP CS Fixer config for Paysera conventions

1034.4k5](/packages/paysera-lib-php-cs-fixer-config)[meklis/switcher-core

SNMP switcher core module

181.6k](/packages/meklis-switcher-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
