Cakephp 3搜索标记
从Cakebook中,我们得到了一个工作示例:Cakephp 3搜索标记,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,从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
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']]);
});
}
例如,使用address articles/taged/test/new,它将查找带有标记“test”或标记“new”的文章。我想用CakePHP的方式修改这段代码,以获得带有标记“test”和标记“new”的文章
你有什么想法吗 以下是您问题的解决方案:更新您的finder查询,如下所示-
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;
}
上面的查询将只返回文章的匹配标记
听起来您正在使用find(标记)和friendsofcake/search插件查找See:)