[TYPO3-mvc] $query->contains: Is there something like $query->containsNone('fieldName')?
Jochen Rau
jochen.rau at typoplanet.de
Sat Mar 13 10:05:33 CET 2010
Hi all.
On 12.03.10 16:24, roberto blanko wrote:
> I am currently experimenting with the new Query::contains() method.
>
> Now $query->contains('categories', $category) gives my all records having
> $categoy assigned. ButI want to select all records having no category
> assigned. In my case the number in the categories field isn't always correct
> (third party reasons) so I can't just use $query->equals('categories', 0).
>
> I've already tried $query->contains('categories', NULL) and
> $query->logicalNot($query->contains('categories')).
>
> Is there any nice solution for that?
Hmm. I recently had a discussion with Karsten about what result to
expect from $query->contains('categories', NULL). We came to the
solution that NULL is never contained in a collection. It depends upon
your domain, if there should be all subjects should be displayed, if
there was no category selected.
Let's wrap up the different cases (what I expect to be the result).
Inside a ProductRepository we specify:
1) $query->matching($query->contains('categories', 'foo'))
Result: Products that have at least the category 'foo' assigned.
2) $query->matching($query->contains('categories', NULL))
Result: empty result set.
3) $query->matching($query->contains(NULL, 'foo'))
Result: Exception (something like "No operand given").
4) $query->matching($query->contains('categories', 'foo'))
with property categories == NULL
Result: empty result set.
5) $query->matching(NULL)
Result: All products (seems obvious but has it's use case in
automatically generated constraints).
Cases 2) and 5) are the edge cases. All cases having NULL as an operand
result in an empty result set. That is IMO the expected behavior because
there *is* a condition specified by contains() but can never be fulfilled.
Back to Robertos use case, which IMO isn't covered by the given
semantics by now. What about having
a) $query->notIsNull('categories')
b) $query->isNull('categories')
c) $query->isEmpty('categories')
d) $query->isUndefined('categories')
e) $query->isNotSet('categories')
a) is according to the JCR specs. The negation by default is a result of
the circumstance that a property that is NULL is non-existent in
repository (JCR).
What's your opinion?
Regards
Jochen
P.S. $query->contains('categories', NULL) is translated to an SQL query
that seems a little bit weird : SELECT * FROM foo WHERE 1<>1. The
snippet "1<>1" ist necessary if there are more than one condition
concatenated with AND or OR.
More information about the TYPO3-project-typo3v4mvc
mailing list