[TYPO3-mvc] How to return a filtered subset of child objects?

Franz Koch typo3.RemoveForMessage at elements-net.de
Sat Feb 13 03:00:49 CET 2010


Hi,

> So, in the end I had no idea where to implement a filter function or
> system, for filtering out unneeded products.
>
> What would be the best place for this? Of course the repository would
> be a very good place for writing custom find methods, but actually I'm
> not sure this would help, as fluid directly calls 'getProducts' or
> 'getDescendentProducts', which will just not call a repository method.

Why don't you move all the logic into the service you already created 
and the model is returning a direct call to the service? I do this in my 
project - but don't know if it's best practice - it's at least better 
then flooding the model with logic where might not belong to.

If you go the way by directly calling the service, I'd suggest to add 
additional getter methods to your model and keep the default ones, if 
you need the opportunity to get all related products. So:
// default one
public function getProducts() {
   return $this->products;
}
// magic one
public function getAvailableProducts() {
   return $this->categoryService->getAvailableProducts($this);
}

And in your service:

public function getAvailableProducts(Tx_YourExt_Domain_Model_Category 
$category) {
   if ($this->applyFilterToProducts()) {
      return 
$this->productRepository->findByCategoryAndCriteria($category, 
$this->getFilterCriteria());
   }
   return $category->getProducts();
}


Maybe something like that?

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list