creocoder / yii2-taggable Goto Github PK
View Code? Open in Web Editor NEWThe taggable behavior for the Yii framework.
License: Other
The taggable behavior for the Yii framework.
License: Other
It's just an idea for a far future. Have you ever thought about tags internalization? Because there are a lot of multilingual projects, but tags are usually in one language.
Hello, Alexander.
I'm now choosing taggable system for a new one project.
What's the main difference between your package and this one?
I saw that you are supporting both of them.
Thanks in advance.
its better to check whether the owner model has error or not before reducing frequency and deleting all relations. because if some behaviors wants to attach error on owner and they registered before TaggableBehavior and reject deleting for some reasons, when deleting is perform the owner model stay live but all relations gone.
something like this in beforeDelete() :
if ($this->owner->hasErrors()) { return; }
we can also think about doing this afterDelete event.
@creocoder I'm not sure about best solution of this issue after last changes: #5
I'm using this behavior config:
[
'class' => TaggableBehavior::className(),
'tagNamesAsArray' => true,
],
In view actions everything working well. But it broke my input widget.
Array to string conversion
<?= $form->field($model, 'tagNames')->textInput() ?>
How to change behavior's config only for update/create actions in controller or in model? Or how to deal with it?
Otherwise we need always have tagNamesAsArray => false
and only set it in true
state when there is a need. Like this:
<?php foreach ($model->getTagNames(true) as $tag) : ?>
<?= Html::a($tag, ['/tag/' . $tag]) ?>
<?php endforeach ?>
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.insertValues
property in Yii1 taggable extension.getAllTagsWithModelsCount
, but this is less critical.Is there tags nesting support planned?
Like:
Furniture
|- Table
| |- Coffee table
And availability to work with tags like with categories. Step-by-step choosing tags: If you have chosen Furniture - you can only choose next tag only from Furniture children.
That way of tags suppose restricting creation of new tags by users automatically (or premoderate them).
MP: lets take this example
Post1 has tag1, tag2, tag3
Post2 has tag1, tag2
post3 has tag1, tag2, tag3
post4 has tag1
post 1 has more in common with post3 then with anything else
then it has partially somethings in common with post2, the last one is post4
if you ever want to show related posts with post1, the order that you need to show is post3, post2, post4
so what I am proposing besides the power of your tag search is to have a related posts relation maybe directly in the behavior, that would take all of the post1 tags, select from the relation table all of the post that have those, make a grouping on post_id and a count on tag_id, order by count(tag_id) and return the posts based on the post_id column
MP: I am not sure it is possible directly in the behavior
AK: I think its possible by TaggableQuery
AK: Just need to think about implementation.
AK: And method name.
AK: I mean it should be scope.
AK: Like tag()
AK: But need to think about that scope name.
AK: Maybe recent()
MP: just some food for thought
AK: Can you create issue with that?
MP: ok, sure
AK: To not forget implement it as future plans.
AK: Because it very serious and interesting feature.
AK: Moreover since behavior is entity related it 100% possible
AK: relatedByTags() maybe...
AK: But we need to think about optimal implementation on SQL level.
MP: we might need too much info put in the behavior...
AK: It will looks like:
Post::find()->relatedByTags(... tags here...)->all();
$post->relatedByTags(... no params here...)->all();
Hi,
there is an issue with the new alias()
function in Yii 2.0.7.
If you use an alias for the primary table the function anyTagValues
in the class TaggableQueryBehavior
returns an error.
I think you should change the function in this way:
public function anyTagValues($values, $attribute = null)
{
$model = new $this->owner->modelClass();
$query = $this->owner;
$tagClass = $model->getRelation($model->tagRelation)->modelClass;
$this->owner
->innerJoinWith($model->tagRelation, false)
->andWhere([$tagClass::tableName() . '.' . ($attribute ?: $model->tagValueAttribute) => $model->filterTagValues($values)])
->addGroupBy(array_map(function ($pk) use ($model,$query) {
return array_search($model->tableName(),$query->from) . '.' . $pk;
}, $model->primaryKey()));
return $this->owner;
}
You don't have to get the table name from $model->tableName()
but from the $from
variable in the ActiveQuery
object.
regards
How to add tagging support for News? Shoud i add taggable behavior to News model or NewsTranslation?
I just tried to have taggable behavior for an yii/redis/activerecord, but it was clear that it won't work since yii/redis/activerecord does not support viaTable() in queries.
I believe its reasonable to use in-memory databases to implement some features that have m2m relations. Taggable behavior is a good case, since it is a m2m relation, data values are short and also searching for tags is a frequent operation throughout application lifetime.
This will make a huge improvement in application performance, so why not to make it compatible with noSQL?
It seems to me that here should not be a negation:
/**
* @return void
*/
public function afterFind()
{
if (!$this->owner->isRelationPopulated($this->tagRelation)) {
return;
}
$this->populateTagNames();
}
correct version, without negation in if statement:
/**
* @return void
*/
public function afterFind()
{
if ($this->owner->isRelationPopulated($this->tagRelation)) {
return;
}
$this->populateTagNames();
}
or
/**
* @return void
*/
public function afterFind()
{
if (!$this->owner->isRelationPopulated($this->tagRelation)) {
$this->populateTagNames();
}
}
Hi
In my news module i'm using this behavior . after updating news it will save tags again although it's tags haven't changed . it doesn't need to save a model when it didn't change.
this made some problems for me .
please help me .
tnx.
I want to use Taggable Behavior for multi table to search like catigories, so how can i do?
Hi.
My post-model::primaryKey() returns ['id', 'version'].
And thus in the TaggableBehavior[:177] rows are of the form array:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[version] => 0
)
[1] => 6
)
)
Similarly this is to remove the tags.
What can be done?
Only override methods of afterSave and beforeDelete?
Update: override as I understand it does not help - private $_tagValues; :(
Have you planned to create a widget to display tags with links on them in view actions or this repository will contain only TaggableBehavior?
IMHO: Separate repository with require will be better solution.
I've had some problems with case sensitivity on inputting tags.
Because of MySQL SELECT statements are not case-sensitive by default: if 'foo' exists as a tag and 'Foo' was inputted, it would would cause a double entry insert statement which because of the composite primary key uniqueness would throw an SQL error and rollback the transaction.
I would suggest adding an attribute or property forcing all incoming tags to lower-case as default.
and when disabled, choose to do a binary mysql statement instead:
SELECT * FROM table
WHERE BINARY column
= 'value
any ideas on this, or do you have any suggestions on how to filter the $tagValues at the MODEL level?
Here is how I'm using tags without SluggableBehavior.
In application config:
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules'=> [
['pattern'=>'tag/<tag>', 'route'=>'tag/view'],
]
],
]
In view file:
<?php foreach ($model->getTagNames(true) as $tag) : ?>
<?= Html::a($tag, ['/tag/view', 'tag' => $tag]) ?>
<?php endforeach ?>
If we have a tag tag with whitespace
we will have a generated link:
/tag/tag+with+whitespace
This link will call TagController:actionView()
method and bypass tag
parameter in Yii::$app->request->queryParams
array.
Как получить все посты с похожими тегами?
relatedByTagValues('Собака') сортирует посты по количеству прикрепленных тегов, а мне нужны в первую очередь посты в которых есть тег 'Собака'.
Post::find()->relatedByTagValues('Собака')->all()
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.
Hi.
I have afterSave() implemented in ActiveRecord which use TaggableBehavior.
And afterSave() from TaggableBehavior dones't have any effect. When I comment my TaggableBehavior works normal.
Any fix for this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.