PHPackages                             creocoder/yii2-taggable - 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. creocoder/yii2-taggable

ActiveYii2-extension

creocoder/yii2-taggable
=======================

The taggable behavior for the Yii framework

2.0.0(11y ago)134131.6k↓14.5%33[12 issues](https://github.com/creocoder/yii2-taggable/issues)[1 PRs](https://github.com/creocoder/yii2-taggable/pulls)9BSD-3-ClausePHP

Since Feb 17Pushed 7y ago23 watchersCompare

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

READMEChangelog (3)Dependencies (1)Versions (4)Used By (9)

Taggable Behavior for Yii 2
===========================

[](#taggable-behavior-for-yii-2)

[![Build Status](https://camo.githubusercontent.com/08650e72c6e518e13d2e62f895619f8d06909dd92e1317b53756f65822c69c1e/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6372656f636f6465722f796969322d7461676761626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/creocoder/yii2-taggable)[![Code Coverage](https://camo.githubusercontent.com/87965f87d0185cdd8b44f61598dc07b820093a75e52eb4acfbb66e97a6196f4c/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6372656f636f6465722f796969322d7461676761626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/creocoder/yii2-taggable/?branch=master)[![Code Quality](https://camo.githubusercontent.com/1217063996e61b42f6015448ecd1c0c75710ef684aa687c28cc22b6dd26873bb/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6372656f636f6465722f796969322d7461676761626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/creocoder/yii2-taggable/?branch=master)[![Packagist Version](https://camo.githubusercontent.com/e486db5cf07bf86ca6997cb1ce0cf60a0ee2d112fa28e7534e3a5ae80891a790/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6372656f636f6465722f796969322d7461676761626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/creocoder/yii2-taggable)

A modern taggable behavior for the Yii framework.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ composer require creocoder/yii2-taggable
```

or add

```
"creocoder/yii2-taggable": "~2.0"

```

to the `require` section of your `composer.json` file.

Migrations
----------

[](#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
-----------

[](#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
-----

[](#usage)

### Setting tags to the entity

[](#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

[](#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

[](#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

[](#remove-all-tags-from-the-entity)

To remove all tags from the entity

```
$post = Post::findOne(1);
$post->removeAllTagValues();
```

### Getting tags from the entity

[](#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

[](#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

[](#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

[](#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

[](#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
--------

[](#donating)

Support this project and [others by creocoder](https://gratipay.com/creocoder/) via [gratipay](https://gratipay.com/creocoder/).

[![Support via Gratipay](https://camo.githubusercontent.com/1c5285a90da2f0b1ee32a7136cacdc9230404760abc467b90492670975ad5bd6/68747470733a2f2f63646e2e7261776769742e636f6d2f67726174697061792f67726174697061792d62616467652f322e332e302f646973742f67726174697061792e737667)](https://gratipay.com/creocoder/)

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity49

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 98.9% 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.

###  Release Activity

Cadence

Every ~4 days

Total

3

Last Release

4101d ago

Major Versions

1.0.1 → 2.0.02015-02-24

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/896494?v=4)[Alexander Kochetov](/maintainers/creocoder)[@creocoder](https://github.com/creocoder)

---

Top Contributors

[![creocoder](https://avatars.githubusercontent.com/u/896494?v=4)](https://github.com/creocoder "creocoder (90 commits)")[![antonkomarev](https://avatars.githubusercontent.com/u/1849174?v=4)](https://github.com/antonkomarev "antonkomarev (1 commits)")

---

Tags

yii2tagsTaggable

### Embed Badge

![Health badge](/badges/creocoder-yii2-taggable/health.svg)

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

###  Alternatives

[sjaakp/yii2-taggable

Manage tags of ActiveRecord in Yii2.

3030.6k](/packages/sjaakp-yii2-taggable)

PHPackages © 2026

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