PHPackages                             2amigos/yii2-taggable-behavior - 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. [Framework](/categories/framework)
4. /
5. 2amigos/yii2-taggable-behavior

AbandonedArchivedYii2-extension[Framework](/categories/framework)

2amigos/yii2-taggable-behavior
==============================

The taggable behavior for the Yii framework

1.0.2(9y ago)86139.5k—3.1%30[1 issues](https://github.com/2amigos/yii2-taggable-behavior/issues)6BSD-3-ClausePHP

Since May 8Pushed 7y ago26 watchersCompare

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

READMEChangelogDependencies (3)Versions (6)Used By (6)

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

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

[![Latest Version](https://camo.githubusercontent.com/44fa8db7c5d9fe3bcb1a71d53d3bd1dbf767fe0e4a49f010938e05c84d40861e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f32616d69676f732f796969322d7461676761626c652d6265686176696f722e7376673f7374796c653d666c61742d737175617265266c6162656c3d72656c65617365)](https://github.com/2amigos/yii2-taggable-behavior/tags)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/be56b582e0373f50e9489975f3b325b74b154467d38cf07f6efffa2528fab77e/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f32616d69676f732f796969322d7461676761626c652d6265686176696f722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/2amigos/yii2-taggable-behavior)[![Coverage Status](https://camo.githubusercontent.com/685dd044439fc574d00dcf4f1bc6ef08d02aaedcd43c429988ac08fd9abc0d54/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f32616d69676f732f796969322d7461676761626c652d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/2amigos/yii2-taggable-behavior/code-structure)[![Quality Score](https://camo.githubusercontent.com/a27f5c37e516c64b1bf46af0b0b575e297c60f10cad007854b22d088ed69f789/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f32616d69676f732f796969322d7461676761626c652d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/2amigos/yii2-taggable-behavior)[![Total Downloads](https://camo.githubusercontent.com/2da4975d005447b0999871199c3fddfa8fbee459dd0fe925752967441646656d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f32616d69676f732f796969322d7461676761626c652d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/2amigos/yii2-taggable-behavior)

This extension provides behavior functions for tagging.

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

[](#installation)

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

Either run

```
$ composer require 2amigos/yii2-taggable-behavior:~1.0
```

or add

```
"2amigos/yii2-taggable-behavior": "~1.0"

```

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

Configuring
-----------

[](#configuring)

First you need to configure model as follows:

```
use dosamigos\taggable\Taggable;

class Tour extends ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => Taggable::className(),
            ],
        ];
    }
}
```

Usage
-----

[](#usage)

First you need to create a `tbl_tag` (you can choose the name you wish) table with the following format, and build the correspondent `ActiveRecord` class (i.e. `Tag`):

```
+-----------+
|  tbl_tag  |
+-----------+
| id        |
| frequency |
| name      |
+-----------+

```

After, if you wish to link tags to a certain `ActiveRecord` (lets say `Tour`), you need to create the table that will link the `Tour` Model to the `Tag`:

```
+-------------------+
| tbl_tour_tag_assn |
+-------------------+
| tour_id           |
| tag_id            |
+-------------------+

```

Next, we need to configure the relationship with `Tour`:

```
/**
 * @return \yii\db\ActiveQuery
 */
public function getTags()
{
    return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('tbl_tour_tag_assn', ['tour_id' => 'id']);
}
```

Its important to note that if you use a different name, the behavior's `$relation` attribute should be changed accordingly.

Finally, setup the behavior, and the attribute + rule that is going to work with it in our `Tour` class, on this case we are going to use defaults `tagNames`:

```
/**
 * @inheritdoc
 */
public function rules()
{
    return [
        // ...
        [['tagNames'], 'safe'],
        // ...
    ];
}

/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        // for different configurations, please see the code
        // we have created tables and relationship in order to
        // use defaults settings
        Taggable::className(),
    ];
}
```

Thats it, we are now ready to use tags with our model. For example, this is how to use it in our forms together with our [Selectize Widget](https://github.com/2amigos/yii2-selectize-widget):

```
// On TagController (example)
// actionList to return matched tags
public function actionList($query)
{
    $models = Tag::findAllByName($query);
    $items = [];

    foreach ($models as $model) {
        $items[] = ['name' => $model->name];
    }
    // We know we can use ContentNegotiator filter
    // this way is easier to show you here :)
    Yii::$app->response->format = Response::FORMAT_JSON;

    return $items;
}

// On our form
