Skip to content

Commit b796df1

Browse files
committed
Refactored TaggableQueryBehavior
1 parent d9b6128 commit b796df1

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/TaggableQueryBehavior.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,44 @@ class TaggableQueryBehavior extends Behavior
2121
{
2222
/**
2323
* Gets entities by any tags.
24-
* @param string|string[] $names
24+
* @param string|string[] $values
25+
* @param string|null $attribute
2526
* @return \yii\db\ActiveQuery the owner
2627
*/
27-
public function anyTagNames($names)
28+
public function anyTagValues($values, $attribute = null)
2829
{
2930
$model = new $this->owner->modelClass();
3031
$tagClass = $model->getRelation($model->tagRelation)->modelClass;
3132

3233
$this->owner
3334
->innerJoinWith($model->tagRelation, false)
34-
->andWhere([$tagClass::tableName() . '.' . $model->tagNameAttribute => $model->filterTagNames($names)])
35+
->andWhere([$tagClass::tableName() . '.' . ($attribute ?: $model->tagValueAttribute) => $model->filterTagValues($values)])
3536
->addGroupBy(array_map(function ($pk) use ($model) { return $model->tableName() . '.' . $pk; }, $model->primaryKey()));
3637

3738
return $this->owner;
3839
}
3940

4041
/**
4142
* Gets entities by all tags.
42-
* @param string|string[] $names
43+
* @param string|string[] $values
44+
* @param string|null $attribute
4345
* @return \yii\db\ActiveQuery the owner
4446
*/
45-
public function allTagNames($names)
47+
public function allTagValues($values, $attribute = null)
4648
{
4749
$model = new $this->owner->modelClass();
4850

49-
return $this->anyTagNames($names)->andHaving(new Expression('COUNT(*) = ' . count($model->filterTagNames($names))));
51+
return $this->anyTagValues($values, $attribute)->andHaving(new Expression('COUNT(*) = ' . count($model->filterTagValues($values))));
5052
}
5153

5254
/**
5355
* Gets entities related by tags.
54-
* @param string|string[] $names
56+
* @param string|string[] $values
57+
* @param string|null $attribute
5558
* @return \yii\db\ActiveQuery the owner
5659
*/
57-
public function relatedByTagNames($names)
60+
public function relatedByTagValues($values, $attribute = null)
5861
{
59-
return $this->anyTagNames($names)->addOrderBy(new Expression('COUNT(*) DESC'));
62+
return $this->anyTagValues($values, $attribute)->addOrderBy(new Expression('COUNT(*) DESC'));
6063
}
6164
}

0 commit comments

Comments
 (0)