5

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

d4ny3l
  • 163
  • 18
  • Do you mean there are multiple categories for each item? Do you sort the items by the number of categories, or by the max category, or something else? – approxiblue Aug 30 '15 at 06:06
  • Yes each item has multiple categories. Each category is a number (int). I want to sort the items according to their category numbers. So for example Item1 has categories[1,2,8], Item2 has categories[1,2,5] so the ascending sort order would be: Item2, Item1 – d4ny3l Aug 31 '15 at 06:15
  • So you sort on the max category number? You should edit your question and add these examples. – approxiblue Aug 31 '15 at 06:16
  • yes alright, I will add the examples to the question. – d4ny3l Aug 31 '15 at 06:23

1 Answers1

2

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...

  • it will "work" but its not what i am looking for. Adding an extra "sorting" property is not possible. I will add a sort function to my ext which parses the object-storage and creates a sorted array, which is then used to make a proper output. – d4ny3l Sep 02 '15 at 06:14