[TYPO3-mvc] How to use <f:groupedFor> when grouping by another domain model class

Henrik Møller Rasmussen info at hmrdesign.dk
Thu Apr 8 15:17:15 CEST 2010


Hello newsgroup,

I'm trying out Extbase and loving it :-)

However I'm having a little trouble getting <f:groupedFor> to work 
correctly.

Imagine two domain model classes - category and product. A category has 
many products and a product has one category. I want to list all 
products grouped by category.



In the controller I do:
---------------------------
$this->view->assign('products',$this->productRepository->findAll());


In the view I then want to do the following:
---------------------------
<f:groupedFor each="{products}" as="productsOfThisCategory" 
groupBy="category" groupKey="category">
	<h1>{category.title}</h1>
	<f:for each="{productsOfThisCategory}" as="product">
		<h2>{product.title}</h2>
	</f:for>
</f:groupedFor>



I know that in this simple example, I could instead loop over all 
categories, and then loop over the products, but this is just a 
simplified edition of my system where I cannot do that.

I have experimented with different syntax but I cannot figure out how to 
do it.

I have succeeded by creating the following method on Product:

public function getCategoryTitle() {
	return $this->getCategory()->getTitle();	
}

and the group by categoryTitle. But this is quite ugly.

Any help is appreciated. Thanks!

Best regards

Henrik Rasmussen


More information about the TYPO3-project-typo3v4mvc mailing list