d4ny3l
d4ny3l

Reputation: 163

Sort elements through child objects

I have an item object with an 1:n relation to categories. Lets say categories is a numeric value.

I tried to sort all items as per categories with setOrderings() but it doesn't work.

//inside findAll() in my ItemRepository 
$query->setOrderings(array("item.categories" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));

How can i get my query to sort all items according to their child objects categories?

EDIT: Example

Item1 has categories[1,2,8], Item2 has categories[1,2,5] so the ascending sort order would be: Item2, Item1

Upvotes: 5

Views: 970

Answers (1)

I think,

$query->setOrderings(array("item.categories" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));

here 'item.categories' is an array variable that's why it is not working.

So you have to create a new element (ex: item.categorie) inside item object for each item (you sort based on the max category number, the value of 'item.categorie' should be this max category number). Now 'item.categorie' will be a normal variable with value like 5 or 8 etc...

Then,

$query->setOrderings(array("item.categorie" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));

Will work...

Upvotes: 2

Related Questions