Search code examples
phpsortingcakephpeager-loadingmodel-associations

CakePHP 3.6.17: Eager loading with sorting


I want to combine eager loading with sort by in cakephp 3.6

Ιn my controller I can use eager loading or sort by but I can't combine them. Here is my code:

Eager loading:

    $user = $this->Users->get($id, [
        'contain' => [ 'TasksTo' => ['ProjectStatus']]
    ]); 
    $this->set(compact('user'));

Sort:

    $user = $this->Users->get($id, [
        'contain' => ['TasksTo'  => ['sort' => ['TasksTo.priority' => 'ASC']]]); 
    $this->set(compact('user'));
}

both work as expected when used separately but not together. How I can combine them?


Solution

  • Well I fixed it like this:

    $user = $this->Users->get($id, [
        'contain' => ['TasksTo' => ['ProjectStatus', 'sort' => ['TasksTo.priority' => 'ASC']]]); 
    $this->set(compact('user'));