1

I have 2 entities: User and Metadata. They have a bi-directional one-to-many relationship (a user can have many metadata records).

In a context decorator I'm using for flagception, I query the user and some of the user's relationships that may be needed using DQL:

$dql = "
    SELECT user
    FROM
        \meQ\Entity\User user
        JOIN user.profile profile
        JOIN profile.client client
        JOIN user.metadata metadata
    WHERE user.uid = :user_id
";

$this->user = $em->createQuery($dql)
    ->setParameter('user_id', $session_user->getId())
    ->useQueryCache(true)
    ->getOneOrNullResult();

// for debug
dump($this->user);
exit();

This query returns the user, complete with the user->profile and user->profile->client fields populated correctly. However, it does not have its metadata field populated.

The user in question has 18 metadata records, but performing dump($this->user) shows me an empty ArrayCollection:

metadata is an empty ArrayCollection

Here is my Doctrine mapping for this relationship:

AppBundle\Entity\User:
    # ...
    oneToMany:
        metadata:
            fetch: EXTRA_LAZY
            targetEntity: AppBundle\Entity\UserMetadata
            mappedBy: user
            cascade: [all]
            orphanRemoval: true
            indexBy: name

and the other side of the relationship:

AppBundle\Entity\UserMetadata:
    # ...
    manyToOne:
        user:
            targetEntity: AppBundle\Entity\User
            inversedBy: metadata
            joinColumn:
                name: user_id
                referencedColumnName: uid

Does this not work because User isn't the owning side?

amacrobert
  • 2,707
  • 2
  • 28
  • 37

1 Answers1

0

I had to explicitly list metadata in my SELECT:

     $dql = "
-        SELECT user
+        SELECT user, metadata
         FROM
             \meQ\Entity\User user
             JOIN user.profile profile
             JOIN profile.client client
             JOIN user.metadata metadata
         WHERE user.uid = :user_id
     ";
amacrobert
  • 2,707
  • 2
  • 28
  • 37