PHPackages                             lav45/yii2-target-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. lav45/yii2-target-behavior

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

lav45/yii2-target-behavior
==========================

This extension allows you to link between multiple objects.

1.3.1(5y ago)22.6k4BSD-3-ClausePHPCI failing

Since May 4Pushed 5y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (10)Used By (0)

Target Behavior for Yii 2
=========================

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

[![Yii2](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](http://www.yiiframework.com/)[![Latest Stable Version](https://camo.githubusercontent.com/fc26074014e1bc243d70038f009847494a1270ee3963ef1800aa184264ce53b1/68747470733a2f2f706f7365722e707567782e6f72672f6c617634352f796969322d7461726765742d6265686176696f722f762f737461626c65)](https://packagist.org/packages/lav45/yii2-target-behavior)[![License](https://camo.githubusercontent.com/654486b686cd526b9eac0ad5cc0d677d4553dd2196b7bb44be6c60875e5421fb/68747470733a2f2f706f7365722e707567782e6f72672f6c617634352f796969322d7461726765742d6265686176696f722f6c6963656e7365)](https://packagist.org/packages/lav45/yii2-target-behavior)[![Total Downloads](https://camo.githubusercontent.com/66bb411a69ec5961fca8c5657dc695460e65e0f474382e0f74ae290a700daa41/68747470733a2f2f706f7365722e707567782e6f72672f6c617634352f796969322d7461726765742d6265686176696f722f646f776e6c6f616473)](https://packagist.org/packages/lav45/yii2-target-behavior)[![Build Status](https://camo.githubusercontent.com/710ed824ba5baa59f378a5db3aa7d47ec5297203bc484e7ab8be3da0c7d865b5/68747470733a2f2f7472617669732d63692e6f72672f4c415634352f796969322d7461726765742d6265686176696f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/LAV45/yii2-target-behavior)

This extension provides behavior functions for linking the two elements through the relation.

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

[](#installation)

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

Either run

```
$ composer require lav45/yii2-target-behavior
```

or add

```
"lav45/yii2-target-behavior": "^1.3"

```

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

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

[](#configuring)

First you need to configure model as follows:

```
use lav45\behavior\Target;

class Post extends ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => Target::className(),
                'targetAttribute' => 'tagNames',
//                'targetRelation' => 'tags',
//                'targetRelationAttribute' => 'name',
//                'delimiter' => ',',
            ],
        ];
    }
}
```

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        |
| 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
        'class' => Target::className(),
        'targetAttribute' => 'tagNames',
    ];
}
```

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)
{
    // We know we can use ContentNegotiator filter
    // this way is easier to show you here :)
    Yii::$app->response->format = Response::FORMAT_JSON;

    return Tag::find()
       ->select(['name'])
       ->where(['like', 'name', $query])
       ->asArray()
       ->limit(10)
       ->all();
}

// On our form
