環境
CakePHP 3.5
前提
posts hasMany commentsの関係とする
やること
commentsのid=123のレコードを取得する
PostsController.php
<?php
namespace App\Conttoller;
class PostsController extends AppController
{
/*
* index method
*/
public function index()
{
$posts = $this->Posts->find()
->contain([
'Comments'
])
->matching(
'Comments', function($q) {
return $q->where([
'id' => 123
]);
});
->order([
'id' => 'ASC'
]);
}
}