LoginSignup
1
1

More than 5 years have passed since last update.

CakePHP3でcontainの中のテーブルをソートする

Posted at

環境

CakePHP 3.5

前提

posts hasMany commentsの関係とする

やること

postsは昇順、commentsは降順でデータを取得する

PostsController.php
<?php
namespace App\Conttoller;

class PostsController extends AppController
{
    /*
     * index method
     */
    public function index()
    {
       $posts = $this->Posts->find()
           ->contain([
               'Comments' => [
                   'sort' => [
                       'id' => 'DESC'
                   ]
               ]
           ])
           ->order([
               'id' => 'ASC'
           ]);
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1