yii2-taggable
yii2-taggable copied to clipboard
Tags with different categories
Such taggable behaviors are a convenient way to store information for attributes that can take an array of values. And sometimes it is convenient to store information about different attributes in the same table using a special column to distinguish, say, category. What would be very useful if this extension allowed to work (save, load, search etc) with multiple tag categories defined for the same model.
This functionality was available in Yii1 through taggable extension by @samdark and was very useful.
You could potentially build on top of existing approach by attaching multiple TaggableBehavior behaviors, one per tag category, and adding a specific where clause like ->where('category = :category', [':category' => $category]); to each relation. But it has certain problems:
TaggableQueryBehaviorright now does not distinguish between owner's behaviors and uses$model->tagRelationwhich would reference a relation from the first bahavior it finds instead of the one we need.- When saving tags, would be nice if tag category was saved automatically. This was handled by
insertValuesproperty in Yii1 taggable extension. - Yii1 taggable extension also provided useful methods like
getAllTagsWithModelsCount, but this is less critical.
It can be done without any additions to current behavior code and attaching multiple behaviors. I'll show how inside Advanced Usage section of documentation.
TaggableQueryBehavior right now does not distinguish between owner's behaviors and uses $model->tagRelation which would reference a relation from the first bahavior it finds instead of the one we need.
This drawback will be fixed. Thanks for catching.
Yii1 taggable extension also provided useful methods like getAllTagsWithModelsCount, but this is less critical.
There is frequency attribute for that. But i'll consider implement method if it setted to false.
I'll show how inside
Advanced Usagesection of documentation.
Looking forward to that! Will it also address the second issue (automatically saving category value)?
There is
frequencyattribute for that. But i'll consider implement method if it setted to false.
Yes, this attribute is very useful. I don't think it worth implementing such method if for some reason user does not want to keep this field in the database.
What I meant is that in Yii1 getting this data was as simple as: $model->behaviorName->getAllTagsWithModelsCount(). Extension took care of filtering by category in where clause.
With this extension it seems like we would need to do something like Tag::find()->where('category = :category', [':category' => $category]). It would just be a bit more convenient if it somehow took care of adding this where clause.