[TYPO3-mvc] How to return a filtered subset of child objects?
Thomas "Thasmo" Deinhamer
thasmo at gmail.com
Sat Feb 13 00:08:24 CET 2010
Good evening,
today I came in a situation, where it was required to filter
the returned child objects of a model object.
I was in need to just return a subset of objects, matching a
certain filter/property value. This filter was set by TS or
via the plugin flexform.
So, the filter value is directly available inside the controller.
Unfortunately I needed to filter objects coming from a
getter method of the object. Think of something like this:
There's a category, holding products. The category model has the
getter 'getProducts' which returns all assigned products. In my
special case, I'd only need a subset of those products, based
on a given property value, which should match the filter.
Furthermore there were two places where those products would
get returned. The first, as I've written already, is the basic
getter method 'getProducts' inside the category model.
The second place is also inside the category model, but it's a
custom method, called 'getDescendentProducts' which will return all
products, assigned to all sub-categories. (So the method will
traverse all the way down, collecting all products and return them.)
As this is some kind of special magic, which may should not
reside in the model object, I wrote a category domain service,
which holds the method 'getDescendentProducts', which will return
all those, of a given category. The service method is then
called inside the model getter method.
Short example of the special getter method:
private getDescendentProducts() {
return $this->categoryService->getDescendentProducts($this);
}
So, I was confronted with the requirement of filtering all returned
products of a category, or even all filtered descendent products.
In the HTML template only the category object itself is assigned,
which results in something like this:
<f:if condition="{category.products}">
<f:for each="{category.products}" as="product">
<h1>{product.title}</h1>
</f:for>
</f:if>
Alternatively with the descendent products:
<f:if condition="{category.descendentProducts}">
<f:for each="{category.descendentProducts}" as="product">
<h1>{product.title}</h1>
</f:for>
</f:if>
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.
A possible solution would be to write a view helper, filtering out
the unneeded objects, but imho I don't think that's what a view helper
should do, or?
Does anybody has a neat solution or an idea about how to implement
such a filter system? I think such a system doesn't exist in such a kind
at present, or did I maybe miss something?
Thanks a lot for your help,
Thomas
More information about the TYPO3-project-typo3v4mvc
mailing list