让我引用文档和测试(如果您在文档中找不到某些内容,测试通常是有关如何做事情的信息的有用来源)。
http://book.cakephp.org/3.0/en/orm/table-objects.html#passing-conditions-to-contain http://book.cakephp.org/3.0/en/orm/table-objects.html#passing-conditions-to-contain
如果您在关联表中定义了一些自定义查找器方法,则可以在包含内使用它们:
// Bring all articles, but only bring the comments that are approved and
// popular.
$query = $articles->find()->contain([
'Comments' => function ($q) {
return $q->find('approved')->find('popular');
}
]);
在这种情况下,您可以简单地传递条件find()
就像您已经在做的那样打电话。
http://book.cakephp.org/3.0/en/orm/table-objects.html#using-the-finder-option http://book.cakephp.org/3.0/en/orm/table-objects.html#using-the-finder-option
https://github.com/cakephp/cakephp/blob/7fc4cfe3ae7d4c523331a44e2862bab5c8f44f1e/tests/TestCase/ORM/QueryTest.php#L2175 https://github.com/cakephp/cakephp/blob/7fc4cfe3ae7d4c523331a44e2862bab5c8f44f1e/tests/TestCase/ORM/QueryTest.php#L2175
所以还有这个“隐藏”finder
可以使用选项来代替可调用的:
$table->find('all')
->where(['Articles.author_id' => $authorId])
->contain([
'Authors' => [
'finder' => ['byAuthor' => ['author_id' => $authorId]]
]
]);
我想如果能在 Cookbook(Cookbook 的文档块)中更详细地记录查找器的使用情况,那不会有什么坏处。Query::contain()
也缺少有关它的信息。