[TYPO3-mvc] How to check in a Query if sub-objects exist?

g4-lisz at tonarchiv.ch g4-lisz at tonarchiv.ch
Thu Oct 13 05:40:51 CEST 2011


On 10/13/2011 03:21 AM, g4-lisz at tonarchiv.ch wrote:
> Hi there,
>
> is there a way of checking just for the existance of sub-objects?
>
> I.e. without writing a SQL query of my own?
>
> Here is a sample to make clear what i mean:
>
>
> public function findByDemand(... $demand) {
>     $query = ...
>
>     if (($property = $demand->getProp1()) !== NULL) {
>         $constraints[] = $query->equals('prop1', $property);
>
>     [etc. for diff. requested properties...]
>
>     if ($demand->getHasPropXY() == true) {
>         $constraints[] = $query->(???);
>     }
>
>     $query->matching($query->logicalAnd($constraints));
>     $query->execute();
> }
>
> $demand::hasPropXY is set bei a checkbox of a multi search form.
>
> The query should return all records which have (sub-)objects of 
> propXY, where propXY  has a 1:n or m:n relation to the parent.
>
> I tried this:
>     if ($demand->getHasPropXY() == true) {
>         $constraints[] = $query->logicalNot($query->equals('propXY', 
> NULL);
>     }
> but the result makes no sense...
>
> In SQL this would be just an inner join without using the fields of 
> the right table...
>
Ok - this actually works:

     if ($demand->getHasPropXY() == true) {
         $constraints[] = $query->logicalNot($query->equals('propXY', 
NULL);
     }

But - and that was the confusion -  $query->count() does not work with this:

     if ($demand->getHasPropXY() == true) {
         $constraints[] = 
$query->logicalNot($query->equals('propXY.propZZ', NULL);
     }

The following code gives a different result than $query->count() or 
count($query):

$count=0;
foreach ($query->execute() as  $whatever) {
     $count++;
}
return $count;

This is rather strange... Somehow the 'DISTINCTion' is not done on 
counting, but later on when returning the single records.
This looks like a bug to me... any opinions?

Cheers,
Till



More information about the TYPO3-project-typo3v4mvc mailing list