creocoder / yii2-taggable

The taggable behavior for the Yii framework.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Many 'tags' relations

usualdesigner opened this issue · comments

Is this possible to attach behavior twice or more? For example imagine that base model "post" has more than one 'tags' relation. It can be "tags", "materials", "locations", "dates" etc.

Yes, its possible.

@creocoder how to pls?

@usualdesigner Just set up behavior twice with different ids, like:

public function behaviors() {
    return [
        'taggable1' => [
            'class' => TaggableBehavior::className(),
        ],
        'taggable2' => [
            'class' => TaggableBehavior::className(),
        ],
    ]
}

In this case there should be ability to overwrite $_tagValues property, isn't it? In my I have two similar relations: tags and materials. They have similar functionality (should works the same). So I need methods like 'getMaterialValues()', 'setMaterialValues()' etc. Is this possible to resolve my objective?

@usualdesigner When you attach same behavior twice it work independently. So you do not need override anything, etc. It will work as it is.

Okay. I've defined two relation:

    public function behaviors()
    {
        return [
            'taggable' => [
                'class' => TaggableBehavior::className(),
                'tagRelation' => 'tags',
                'tagValueAttribute' => 'title',
            ],
            'materialable' => [
                'class' => TaggableBehavior::className(),
                'tagRelation' => 'materials',
                'tagValueAttribute' => 'title',
            ],
        ];
    }

Likewise my model has the relations below:

public function getTags()
    {
        return $this->hasMany(Tag::className(), ['tag_id' => 'tag_id'])
            ->viaTable('{{%items_tags}}', ['item_id' => 'item_id']);
    }

    public function getMaterials()
    {
        return $this->hasMany(Material::className(), ['material_id' => 'material_id'])
            ->viaTable('{{%items_materials}}', ['item_id' => 'item_id']);
    }

How in this case I can define tags and materials? I can define tags like this:

$item = new Item();
$item->tagValues = 'one, two, three';

But I can't define materials.

@usualdesigner Example:

$item = new Item();
$item->getBehavior('taggable')->tagValues = 'one, two, three';

$item = new Item();
$item->getBehavior('materialable')->tagValues = 'one, two, three';

Many thanks. That's works.

Another question related to other:

if I have two 'taggable' behaviors in my model - how to use 'relatedByTagValues()' and others similar functions? What if I need to filter by tags and by materials in my own case?

commented

how can access multi behavior in view ?
i use selectize widget and it is worked but when want to add another widget dont know how access it becuse tagValues is uniqe
`

field($model, 'tagValues')->widget(dosamigos\selectize\SelectizeTextInput::classname(), [ 'loadUrl' => '/tag/list', 'options' => [ 'placeholder' => Yii::t('app', 'Add Tags'), 'class' => 'form-control', ], 'clientOptions' => [ 'persist' => false, 'plugins' => ['remove_button'], 'create' => true, 'maxItems' => 3, 'valueField' => 'name', 'labelField' => 'name', 'searchField' => ['name'], ], ])->hint('Use commas to delimit zip codes')->label(Yii::t('app', 'Category')); ?>

`