image

How to sort associated data?

Today quick tip. Do you know that you can sort associated data and everything is available from scratch?

Let's take an example product entity and we want to add properties and properties groups as associated data. After all, we want to sort those properties by order field in property groups.

To add specific associations to the results we have to add them to the criteria.

$criteria = new Criteria();
$criteria->addAssociation('properties');
$criteria->addAssociation('properties.group');
To sort associated data you have to get this association and execute the sort method on it.
$criteria->getAssociation('properties')->addSorting(new FieldSorting('group.position', 'DESC'));

Did you know about this or is it something new for you?

Leave a Comment