yii2-taggable icon indicating copy to clipboard operation
yii2-taggable copied to clipboard

Tags with different categories

Open nirvana-msu opened this issue 10 years ago • 5 comments

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:

  • 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.
  • When saving tags, would be nice if tag category was saved automatically. This was handled by insertValues property in Yii1 taggable extension.
  • Yii1 taggable extension also provided useful methods like getAllTagsWithModelsCount, but this is less critical.

nirvana-msu avatar Jan 27 '15 21:01 nirvana-msu

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.

creocoder avatar Jan 27 '15 21:01 creocoder

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.

creocoder avatar Jan 27 '15 21:01 creocoder

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.

creocoder avatar Jan 27 '15 21:01 creocoder

I'll show how inside Advanced Usage section of documentation.

Looking forward to that! Will it also address the second issue (automatically saving category value)?

nirvana-msu avatar Jan 27 '15 22:01 nirvana-msu

There is frequency attribute 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.

nirvana-msu avatar Jan 27 '15 22:01 nirvana-msu