Hi Dennis,

On 6 May 2016 at 21:11, Dennis Fedco <[email protected]> wrote:

> *Peculiarity #1*
>
> I have some code, like so:
>
>
>         $query = $this->getEntityManager()->createQuery('
>             SELECT i, p
>             FROM ' . Item::class . ' i
>             INNER JOIN ' . Product::class . ' p
>             WHERE
>                     i.productId = p.id
>         ');
>         $result = $query->getResult();
>
>         foreach ($result as $row)
>             print get_class($row) . ' - ' . $row->getId() . "\n"
>
>
The "WHERE i.productId = p.id" seems to be incorrect here. Is there an
association between Product and Item? If so, then you can simply rewrite
the query as following:

'SELECT i, p
FROM ' . Item::class . ' i
JOIN i.product p'

This already hydrates the Product instance into the Item objects being
retrieved.


> I get this output, which is a one-dimensional array (I've added array
> indices for clarity):
>
> [0] => Application\Entity\Item - 284
> [1] => DoctrineProxies\__CG__\Application\Entity\Product - 59
> [2] => Application\Entity\Item - 302
> [3] => Application\Entity\Item - 288
> [4] => DoctrineProxies\__CG__\Application\Entity\Product - 58
> [5] => Application\Entity\Item - 292
> [6] => DoctrineProxies\__CG__\Application\Entity\Product - 57
>
> I found this to be peculiar because I am requesting both "i", and "p" in
> my select statement but I get count of four of "Item" returned and count of
> 3 of "Product".
> Namely, I would typically expect 4 and 4.  That is, I'd expect this line
> to be present:
> [2.5] => DoctrineProxies\__CG__\Application\Entity\Product - 59 --- That
> line is *missing* in my output.
>
> (Index of 2.5 to be interpreted as a made up index to signify that the
> line should have been the one between indices 2 and 3)
>
> I suspect it is because the index of the Product happens to be the same in
> the join, 59 in this case)
> But then I have to craft my code to accommodate for this, because normally
> I'd expect the Item and Product to alternate.
>
> Is this behavior "by design"?
>

Can you please check the SQL results and compare them to the hydrated
result? As it stands, from this perspective (reader) I can't say what the
expectations would be.
The output is indeed messy due to how you decided to hydrate it (fetch
join, but on an arbitrary join)


> *Peculiarity #2*
>
> If I change the above DQL to say "p.model" instead, keeping everything
> else the same.  Like so:
> SELECT i, p.model
>
> Instead of an array of N Objects or Proxies, I now get an array of
> arrays.  See below (with indices for clarity):
> [0][0] => Application\Entity\Item - 284
> [0][1] => AA (model for id==59)
> [1][0] => Application\Entity\Item - 302
> [1][1] => AA (model for id==59)
> [2][0] => Application\Entity\Item - 288
> [2][1] => BB (model for id==58)
> [3][0] => Application\Entity\Item - 292
> [3][1] => CC (model for id==57)
>
> The *inconsistency* between the structure of result sets gotten from
> similar DQL statements is confusing and unexpected to me.
>

This seems to be the correct result instead. Try `i as item, p.model as
model`, should produce better results.


>
> Is this behavior by design?
>

The first result (all mixed results in a one-dimension array) is most
likely NOT what we want as a result, and may be a bug. I do not think this
will be fixed soon though, as somebody may rely on it at this time.
Instead, we might get rid of this kind of behavior in the next major
iteration.


> Am I using ORM correctly in this such cases?
>

I think the lack of a fetch-join over an association is what is the most
confusing bit about the initial query. Please try applying the DQL I
suggested above.


> My goal is to get the contents of Item, but also pull model from Product
>

Yes, that would be a fetch-join operation over an association.

Cheers,

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to