[TYPO3-mvc] Extbase query with AND over 1:n related objects

Jochen Rau jochen.rau at typo3.org
Thu Mar 29 16:52:53 CEST 2012


Hi Till,

On Thu, Mar 29, 2012 at 01:10:26PM +0200, g4-lisz at tonarchiv.ch wrote:
> Object from class A (e.g. "book") has a property which is related
> 1:n to class B (e.g. "category"). One book can belong to more than
> one categories.

Wouldn't that be m:n relation? A book can be assigned to many
categories, and a category can be assigned to many books.

> Now i like to do a query which finds all books with category B=1 AND
> B=2 i.e. the book has to belong to both categories.
> 
> If i do something like this:
> 
>                 if (count($demand->getCategories()) > 0) {
>                         foreach ($demand->getCategories() as $category)
>                                 $constraints[] =
> $query->equals('book.category.uid', $category);
>                 }
>               $query->matching($query->logicalAnd($constraints));
> 
> then i get all books which belong to one of the categories (same
> like " $constraints[] = $query->in('book.category.uid',
> $demand->getCategories());")

Don't use uids as constrains (unless you really know what you are doing ;-)).

What about

$query->contains('book.categories', $category);

-Jochen


More information about the TYPO3-project-typo3v4mvc mailing list