@@ -21,41 +21,44 @@ class TaggableQueryBehavior extends Behavior
21
21
{
22
22
/**
23
23
* Gets entities by any tags.
24
- * @param string|string[] $names
24
+ * @param string|string[] $values
25
+ * @param string|null $attribute
25
26
* @return \yii\db\ActiveQuery the owner
26
27
*/
27
- public function anyTagNames ( $ names )
28
+ public function anyTagValues ( $ values , $ attribute = null )
28
29
{
29
30
$ model = new $ this ->owner ->modelClass ();
30
31
$ tagClass = $ model ->getRelation ($ model ->tagRelation )->modelClass ;
31
32
32
33
$ this ->owner
33
34
->innerJoinWith ($ model ->tagRelation , false )
34
- ->andWhere ([$ tagClass ::tableName () . '. ' . $ model ->tagNameAttribute => $ model ->filterTagNames ( $ names )])
35
+ ->andWhere ([$ tagClass ::tableName () . '. ' . ( $ attribute ?: $ model ->tagValueAttribute ) => $ model ->filterTagValues ( $ values )])
35
36
->addGroupBy (array_map (function ($ pk ) use ($ model ) { return $ model ->tableName () . '. ' . $ pk ; }, $ model ->primaryKey ()));
36
37
37
38
return $ this ->owner ;
38
39
}
39
40
40
41
/**
41
42
* Gets entities by all tags.
42
- * @param string|string[] $names
43
+ * @param string|string[] $values
44
+ * @param string|null $attribute
43
45
* @return \yii\db\ActiveQuery the owner
44
46
*/
45
- public function allTagNames ( $ names )
47
+ public function allTagValues ( $ values , $ attribute = null )
46
48
{
47
49
$ model = new $ this ->owner ->modelClass ();
48
50
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 ))));
50
52
}
51
53
52
54
/**
53
55
* Gets entities related by tags.
54
- * @param string|string[] $names
56
+ * @param string|string[] $values
57
+ * @param string|null $attribute
55
58
* @return \yii\db\ActiveQuery the owner
56
59
*/
57
- public function relatedByTagNames ( $ names )
60
+ public function relatedByTagValues ( $ values , $ attribute = null )
58
61
{
59
- return $ this ->anyTagNames ( $ names )->addOrderBy (new Expression ('COUNT(*) DESC ' ));
62
+ return $ this ->anyTagValues ( $ values , $ attribute )->addOrderBy (new Expression ('COUNT(*) DESC ' ));
60
63
}
61
64
}
0 commit comments