gpt4 book ai didi

php - 如何在 CakePHP 中使用 matching() 查找具有共同父级的实体?

转载 作者:行者123 更新时间:2023-12-02 06:54:01 24 4
gpt4 key购买 nike

我需要找到与给定文章列表具有相同作者的所有文章

这是我的自定义查找器方法:

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]);
});
});
}

不幸的是,我收到以下错误消息:PDOException:SQLSTATE[42000]:语法错误或访问冲突:1066 不是唯一的表/别名:“文章”我做错了什么?

最佳答案

嵌套匹配有很多限制,这可能是其中之一。我不知道它是否可能是一个错误,所以你可能想检查 issues on GitHub并最终提交一份新文件以进行澄清。

引用自文档:

[...] Dotted matching paths should be used over nested matching() calls [...]

Cookbook > Database Access & ORM > Retrieving Data & Results Sets > Filtering by Associated Data

在任何一种情况下,使用点符号而不是嵌套应该可以解决问题,即

return $query->matching('Authors.Articles', function ($q) use ($idList) {
return $q->where(['Articles.id IN' => $idList]);
});

如果你还想匹配 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]);
});

关于php - 如何在 CakePHP 中使用 matching() 查找具有共同父级的实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35685421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com