CakePHP 3:[ManyToMany] 获取带有多个特定标签的书签

CakePHP 3: [ManyToMany] Getting Bookmarks with MULTIPLE specific tags

我想在此处存档与书签教程非常相似的内容:https://book.cakephp.org/3/en/tutorials-and-examples/bookmarks/intro.html#getting-bookmarks-with-a-specific-tag

查找带有标签 "funny"、"cat"、"gifs" 的书签 此查询在 BookmarksController 中可用:

$tags = ['funny','cat','gifs'];
$bookmarks = $this->Bookmarks->find();
  ->innerJoinWith('Tags')
  ->where(['Tags.title IN ' => $tags]);
  ->group(['Bookmarks.id']);

$this->set('result',$tags);

这 return 书签标有有趣的 OR cat OR gifs。

我正在尝试将其更改为 return 只有标有有趣 AND cat AND gif 的书签。

有人知道如何存档吗?

谢谢,我让它工作了。就是这样

$current_tags = ['funny','cat','gifs']
$query = $this->Bookmarks->find()->contain(['Tags'])
            ->matching('Tags', function($query) use ($current_tags){
                return $query->where(['Tags.name IN' => $current_tags]);
            });
            $query->group('Bookmarks.id')->having([
        $this->Bookmarks->query()->newExpr('COUNT(DISTINCT Tags.name) = '.count($current_tags))
    ]);

感谢 我解决了我在遵循上面的评论答案后遇到的问题。使用数组变量进行搜索时会出现问题。在本例中为 $current_tags.

matching([...]) 正在使用匿名函数。 匿名函数有一个新的作用域。这就是为什么必须使用 use().

继承变量的原因