All Projects → 2amigos → Yii2 Taggable Behavior

2amigos / Yii2 Taggable Behavior

Licence: other
This extension allows you to get functional for tagging.

Projects that are alternatives of or similar to Yii2 Taggable Behavior

yii2-linkable-behavior
Yii2 behavior to help creating urls easier
Stars: ✭ 12 (-85.54%)
Mutual labels:  behavior, yii2
behavior-trait
Allows handling events via inline declared methods, which can be added by traits
Stars: ✭ 18 (-78.31%)
Mutual labels:  behavior, yii2
Yii2 Save Relations Behavior
Validate and save automatically related Active Record models.
Stars: ✭ 125 (+50.6%)
Mutual labels:  yii2, behavior
yii2-db
Database extensions for Yii 2.0 Framework 📦
Stars: ✭ 19 (-77.11%)
Mutual labels:  behavior, yii2
Yii2 Async
Provides translucent api & queues for moving large tasks out of request context with SQL, Redis or AMQP.
Stars: ✭ 64 (-22.89%)
Mutual labels:  yii2
Lang Sc
一本字典
Stars: ✭ 57 (-31.33%)
Mutual labels:  yii2
Yii2 Sortable Widgets
🍨 Rubaxa/Sortable for Yii2
Stars: ✭ 54 (-34.94%)
Mutual labels:  yii2
Yii2 Quill
Yii 2 implementation of Quill, modern WYSIWYG editor
Stars: ✭ 52 (-37.35%)
Mutual labels:  yii2
Gui Rbac Yii2
GUI for RBAC Yii2 Auth manager
Stars: ✭ 83 (+0%)
Mutual labels:  yii2
Yii2 Schemadump
Generate the schema from an existing database.
Stars: ✭ 78 (-6.02%)
Mutual labels:  yii2
Yii2 Jwt
JWT implementation for Yii2 Authorization process
Stars: ✭ 61 (-26.51%)
Mutual labels:  yii2
Yii2 Psr Log Target
Yii 2.0 log target that is able to write messages to PSR-3 compatible logger
Stars: ✭ 58 (-30.12%)
Mutual labels:  yii2
Yii2 Smarty
Yii 2 Smarty Extension.
Stars: ✭ 67 (-19.28%)
Mutual labels:  yii2
Php frameworks analysis
php框架源码分析
Stars: ✭ 57 (-31.33%)
Mutual labels:  yii2
Csv Grid
Yii2 extension for CSV export
Stars: ✭ 83 (+0%)
Mutual labels:  yii2
Config
Yii2 application runtime configuration support
Stars: ✭ 54 (-34.94%)
Mutual labels:  yii2
Yii2 Collection
Collection extension for Yii 2
Stars: ✭ 62 (-25.3%)
Mutual labels:  yii2
Kantphp2
KantPHP Framework V2
Stars: ✭ 75 (-9.64%)
Mutual labels:  yii2
Sitemap
Site map creation support
Stars: ✭ 59 (-28.92%)
Mutual labels:  yii2
Yii2 Configloader
Build configuration arrays from config files and env vars.
Stars: ✭ 59 (-28.92%)
Mutual labels:  yii2

Taggable Behavior for Yii 2

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

This extension provides behavior functions for tagging.

Installation

The preferred way to install this extension is through composer.

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

First you need to configure model as follows:

use dosamigos\taggable\Taggable;

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

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:

// 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
<?= $form->field($model, 'tagNames')->widget(SelectizeTextInput::className(), [
    // calls an action that returns a JSON object with matched
    // tags
    'loadUrl' => ['tag/list'],
    'options' => ['class' => 'form-control'],
    'clientOptions' => [
        'plugins' => ['remove_button'],
        'valueField' => 'name',
        'labelField' => 'name',
        'searchField' => ['name'],
        'create' => true,
    ],
])->hint('Use commas to separate tags') ?>

As you can see, tagNames is the attribute (by default) from which we can access our tags and they are stored in it as names separated by commas if you defined your attribute tagNames as string or null, if you define tagNames as an array, it will be filled with the related tags.

Once you post a form with the above field, the tags will be automatically saved and linked to our Tour model.

Testing

$ ./vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The BSD License (BSD). Please see License File for more information.


web development has never been so fun
www.2amigos.us
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].