[TYPO3-mvc] Fetching objects from repository having specific related objects

Christian Zenker christian.zenker at 599media.de
Fri Sep 10 15:09:51 CEST 2010


Hi.

On Fri, 10 Sep 2010 14:45:35 +0200, Elmar A. <oxyfodu at googlemail.com>  
wrote:
> Hi
>
> The relevant parts of my domain model look like this (in  
> pseudo-ascii-uml):
>
> Category 1 <---- * Subcategory 1 <---- * Collection * <----- * Product
>
> Now, I want to fetch all categories (and subcategories, collections)
> with products whose property "gender" is set to "f" or "m".

Have you tried something like this?
$query->in('subcategory1.collection.product', array('f','m'));
(or whatever you have called the relations in TCA)

> This seems to be sensible as I'm using e.g.
> $categories->getSubcategories() and it brings me ALL subcategories of
> the category, no only the subcategories from the original query.

Do you use lazy loading? I would guess that the related objects get  
instanciated right away if you DON'T use lazy loading. But I'm not sure  
how extbase handles this. After all, you should report this as a bug (if  
it was not already done) - but don't expect it to be fixed soon. As far as  
I understand it, you have to dig very deep into extbase to fix this.
I guess the best solution is to take care of fetching the right objects  
yourself. I do it like that:

/**
  * @todo fix workaround
  * @var Tx_Extbase_Persistence_ObjectStorage
  **/
protected $_foobar = null;

public function getFoobar() {
	if(is_null($this->_foobar)) {
		// instanciate repo
		$this->_foobar = $repo->findSomethingBySomething($this->getUid);
	}
	return $this->_foobar;
}

You will get in trouble with this workaround when trying to update  
relations, but in other cases this is a clean workaround.

> I can't really think of a good way to solve this problem:
> - I could check in the template if I want to output a
> category/collection etc., but the decision WHAT to display belongs to
> the controller IMO

right

> - traversing all categories (in PHP Code), I could remove all
> subcategories/collections etc. without products in question. But using
> the Tx_Extbase_Persistence_ObjectStorage's detach method would persist
> the changes (if I get the docs correctly)

That's right. But you could create a simple array where you *insert* all  
objects you want to use.


More information about the TYPO3-project-typo3v4mvc mailing list