Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order 'Contain' Model in CakePHP 3.x

I have multiple Things in AnotherThing now I am trying to order them in my edit action in my AnotherThing Controller - I can access the Things just fine in my edit, but I want to sort them differently (not over their ID), for example Things.pos

Whats the best practice here? I tried it with

public $hasMany = array(
        'Thing' => array(
            'order' => 'Thing.pos DESC'
        )
    );

But nothing changed. Any idea?

like image 281
Isengo Avatar asked Jul 05 '16 13:07

Isengo


People also ask

What is contain in CakePHP?

A new addition to the CakePHP 1.2 core is the ContainableBehavior . This model behavior allows you to filter and limit model find operations. Using Containable will help you cut down on needless wear and tear on your database, increasing the speed and overall performance of your application.

How can I get query string in CakePHP?

In CakePHP 2.0 this appears to have changed. According to the documentation you can access $this->request->query or $this->request['url'] .


1 Answers

Overread the Documentation. Sorry!

Anyways, If someone looks for an answer, here it is: http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#sorting-contained-associations

edit:

When loading HasMany and BelongsToMany associations, you can use the sort option to sort the data in those associations:

$query->contain([
    'Comments' => [
        'sort' => ['Comment.created' => 'DESC']
    ]
]);
like image 151
Isengo Avatar answered Sep 22 '22 14:09

Isengo