Thank you in advance for anyone who reads this and takes the time to 
respond!

I have been reading a lot lately about Doctrine best practices, and I am 
interested in removing some lazy-loaded relationships on my models and 
using single, one-time queries in Repository classes.

For example, my domain model has 2 entities: Event, and EventTicket

EventTicket has a ManyToOne relationship to Event:

class EventTicket {
    /**
     * @ORM\ManyToOne(targetEntity="Event")
     * @ORM\JoinColumn(name="event_id", referencedColumnName="id")
     */
    $event;
    ...
}

I would like to write a query that gets all events and also all event 
tickets for those events, such that the result looks something like:

array (size=5)
  0 => 
    array (size=6)
      'id' => int 1
      'name' => string 'Linux User Group Meetup' (length=23)
      'startsAt' => string '2016-10-08 20:00:00' (length=19)
      'endsAt' => string '2016-10-09 02:00:00' (length=19)
      'hashtag' => string 'freegeek' (length=8)
      'tickets' => array(
           array(
                  'id' => 1,
                  'name' => 'Ticket name',
                  ...
           ),
           array(
                  'id' => 2,
                  'name' => 'Ticket 2 name',
                  ...
           )
      )
  ...
)

But, *I want to avoid adding an inverse side relationship 'tickets' onto 
the Event entity* to avoid lazy loading due to and in general just to 
follow the best practice suggestions on Doctrine's docs about trying to 
avoid bi-directional relationships, etc.

So, my query looks like this (sorry for the formatting):

class EventRepository extends EntityRepository
{
    public function findAll()
    {
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->select('e', 't')
            ->from(Event::class, 'e')
            ->join(EventTicket::class, 't', Join::WITH, 't.event = e')
            ;

        $query = $qb->getQuery();
        Debug::dump($query->getArrayResult());

...

Which returns me a result that looks like this:

array (size=5)
  0 => *// This is my Event object*
    array (size=6)
      'id' => int 1 
      'name' => string 'Linux User Group Meetup' (length=23)
      'startsAt' => string '2016-10-08 20:00:00' (length=19)
      'endsAt' => string '2016-10-09 02:00:00' (length=19)
      'hashtag' => string 'freegeek' (length=8)
      'address' => string '1731 SE 10th Ave, Portland, OR 97214, United 
States' (length=51)
  1 => *// This is the first EventTicket*
    array (size=9)
      'id' => int 1
      'name' => string 'Paid' (length=4)
      'description' => string '' (length=0)
      'quantity' => int 100
      'price' => string '10.00' (length=5)
      'startsAt' => null
      'endsAt' => null
      'minPerOrder' => int 1
      'maxPerOrder' => null
  2 => *// This is the second EventTicket*
    array (size=9)
      'id' => int 2
      'name' => string 'Free' (length=4)
      'description' => string '' (length=0)
      'quantity' => int 200
      'price' => string '0.00' (length=4)
      'startsAt' => null
      'endsAt' => null
      'minPerOrder' => int 1
      'maxPerOrder' => null

This is as expected, since Doctrine's docs suggest this is what the result 
set will look like when querying for multiple 
entities: 
http://doctrine-orm.readthedocs.io/en/latest/reference/dql-doctrine-query-language.html#fetching-multiple-from-entities

*Is there any way that I can nest these 2 EventTicket arrays underneath the 
root Event array with a 'tickets' key using DQL?* This would make it much 
easier for me when I go to serialize this array. If this is not possible, I 
will have to figure out a way to loop through these values and transform 
the arrays so for serialization...

Again, thank you in advance.

-- 
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