[TYPO3-mvc] Selecting records which have MM relations to another table

Jochen Rau jochen.rau at typoplanet.de
Fri Dec 18 23:44:06 CET 2009


Hi Jan-Erik,

schrieb Jan-Erik Revsbech:
[...]
> Now I need to make an action on my Documentcontroller that lists all
> Documents in a given category (or several categories actually). How do
> I do
> this? Using
>
> $this->documentRepository =
> t3lib_div::makeInstance('Tx_applusDocument_Domain_Repository_DocumentReposit
>
> ory');
>
> $Query = $this->documentRepository->createQuery();
>
> $Query->matching($Query->equals('category',$category));
>
> I hopefully (and a little naively) expetected to get the document which
> have $category as their category? However this is is not the case. Do I
> somehow need to join the MM table on the query?

I assume that you have a property "categories" in your "Document" 
object. Then you should be able to write something similar to:

$query->matching($query->equals('categories', $category))->execute();

But that doesn't work as yet. See: http://forge.typo3.org/issues/show/4679.

So what to do meanwhile? A possible solution is to write a Query with 
joint tables. But that's a little bit awkward by now (have a look at 
DataMapper->getPreparedQuery() for an example).

As a fall-back you can use

$query->statement('SELECT * FROM tx_myext_foo LEFT JOIN ....')->execute();

I'll dig a little bit deeper into this issue the next few days.

Regards
Jochen


-- Every nit picked is a bug fixed



More information about the TYPO3-project-typo3v4mvc mailing list