I know how to sort the primary model using the ‘order’ option. But how do I sort associated data?
Ok, I tried this:
$task = $this->Task->read(null, $id, array('order' =>
array('Subtask.completed ASC')
));
Then I found out that read doesn’t have an option parameter. So I tried setting a variable for the other model, so I could use the option in the find method:
$subtasks = $this->Subtask->find('all', array('conditions' => array(
'Subtask.task_id' => $id),
'order' => array('Subtask.completed ASC')
));
Which got me the following error:
Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\taskExplorer\app\controllers\tasks_controller.php on line 54
So a solution to the problem might be to bind in a second model (Subtask) so I can use it for find. But I don’t know how to do that.
Try this :
And if you have defined relationship in model then you can access it like: