从Cakebook中,我们得到了以下工作示例:
public function findTagged(Query $query, array $options) {
return $this->find()
->distinct(['Articles.id'])
->matching('Tags', function ($q) use ($options) {
if (empty($options['tags'])) {
return $q->where(['Tags.title IS' => null]);
}
return $q->where(['Tags.title IN' => $options['tags']]);
});
}例如,使用地址文章/ tag /test/new,它将查找标签为"test“或标签为"new”的文章。我想以测试的方式修改此代码,以获得带有标签“CakePHP”和标签"new“的文章。
你有什么想法吗?
发布于 2017-03-16 17:59:04
以下是您问题的解决方案:按如下方式更新您的查询-
public function findTagged(Query $query, array $options){
$this->opt = $options['tags'];
$articles = $this->find()
->select(['Articles.id', 'Articles.url', 'Articles.title', 'Articles.description'])
->matching('Tags', function ($query) {
return $query->where(['Tags.title IN' =>$this->opt]);
})->group(['Articles.id'])
->having([
$this->Users->query()->newExpr('COUNT(DISTINCT Tags.title) = '.count($this->opt))
]);
return $articles;
}上面的
查询将只返回文章的
matching tags。
https://stackoverflow.com/questions/42731950
复制相似问题