Ben*_*ási 2 php cakephp cakephp-3.x cakephp-3.2
我需要找到与给定文章列表具有相同作者的所有文章
这是我的自定义查找器方法:
public function findSimilar(Query $query, array $options)
{
if (empty($options['idList'])) {
throw new Exception('idList is not populated');
}
// We are given a list of article IDs
$idList = $options['idList'];
return $query->matching('Authors', function ($q) use ($idList) {
return $q->matching('Articles', function ($q2) use ($idList) {
return $q2->where(['Articles.id IN' => $idList]);
});
});
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我收到以下错误消息:PDOException:SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Articles'
我做错了什么?
嵌套匹配有很多限制,这可能是其中之一.我不知道它是否可能是一个错误,所以你可能想检查GitHub上的问题,并最终提交一个新的澄清.
从文档引用:
[...]应该在嵌套的匹配()调用[...]上使用虚线匹配路径
Cookbook>数据库访问和ORM>检索数据和结果集>按关联数据过滤
在任何一种情况下,使用点符号而不是嵌套应该解决问题,即
return $query->matching('Authors.Articles', function ($q) use ($idList) {
return $q->where(['Articles.id IN' => $idList]);
});
Run Code Online (Sandbox Code Playgroud)
如果你想要匹配Authors,你可以堆叠匹配器,比如
return $query
->matching('Authors', function ($q) {
return $q->where(['Authors.foo' => 'bar']);
})
->matching('Authors.Articles', function ($q) use ($idList) {
return $q->where(['Articles.id IN' => $idList]);
});
Run Code Online (Sandbox Code Playgroud)