[TYPO3-mvc] Extbase query with AND over 1:n related objects
g4-lisz at tonarchiv.ch
g4-lisz at tonarchiv.ch
Thu Mar 29 19:42:31 CEST 2012
Hi Jochen
On 03/29/2012 04:52 PM, Jochen Rau wrote:
> 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.
>
Of course you're absolutely right!
>
>> 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 ;-)).
>
Ah ok. That was because the $demand->getCategories() returns an array
with UIDs from a mutli checkbox form,,,
> What about
>
> $query->contains('book.categories', $category);
>
This gives the same result (also books with only one of the given
categories).
What i need is all the bookes with all of the given categories. e.g.
books which are "classic" and "novel" at the same time...
Of course i could get the result set for each category and
join/intersect them afterwards. But this is slow and inelegant.
Cheers,
Till
More information about the TYPO3-project-typo3v4mvc
mailing list