Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2536

AbstractQuery::setFetchMode not working as expected

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 2.3.4
    • Fix Version/s: None
    • Component/s: None
    • Security Level: All
    • Labels:
      None

      Description

      Let's have theese entities

      class Order
      { // $id, ...
      	/** @ORM\OneToMany(targetEntity="OrderItem", mappedBy="order") */
      	protected $items;
      }
      
      class OrderItem
      { // $id, ...
      	/** @ORM\ManyToOne(targetEntity="Order", inversedBy="items") */
      	protected $order;
      
      	/** @ORM\OneToMany(targetEntity="OrderItemAddition", mappedBy="orderItem") */
      	protected $additions;
      }
      
      class OrderItemAddition
      {
      	/** @ORM\Id() @ORM\ManyToOne(targetEntity="OrderItem", inversedBy="additions", fetch="LAZY") */
      	protected $orderItem;
      
      	/** @ORM\Id() @ORM\ManyToOne(targetEntity="Addition", inversedBy="orderItems", fetch="EAGER") */
      	protected $addition;
      }
      
      class Addition
      { // $id, $name, ...
      }
      

      Now let's have a query

      $qb = $repository->createQueryBuilder('o');
      $qb->addSelect('i')
      	->innerJoin('o.items', 'i')
      	->orderBy('o.id', 'DESC');
      
      $q = $qb->getQuery();
      $q->setFetchMode('Damejidlo\Orders\OrderItem', 'additions', ClassMetadata::FETCH_EAGER);
      $q->setFetchMode('Damejidlo\Orders\OrderItemAddition', 'addition', ClassMetadata::FETCH_EAGER);
      $q->setFetchMode('Damejidlo\Orders\Order', 'corporateMember', ClassMetadata::FETCH_EAGER);
      

      The query is ok, data is ok, BUT. This generates an awful amount of queries. I would expect 5 at most (1 count, 1 data fetch, 3 for eagers).

      And no, I don't want to join more table in one query, because that's awfully unefective and makes me transfer too much data repeatedly (order every orderItem and again for every addition).

      It seems to be working partially. It fetches all OrderItemAddition in one query, but not those joined to them (Addition entity).

      I believe this should generate one query for every entity type as I specified, not one for each row.

      I also believe, you've already had talk with author of NotORM (now you probably know exactly where this is heading).

        Activity

        Hide
        Benjamin Eberlei added a comment -

        eager fetching across entities is not yet implemented for collections. Its still deep in my mind somewhere that this needs to be done

        Show
        Benjamin Eberlei added a comment - eager fetching across entities is not yet implemented for collections. Its still deep in my mind somewhere that this needs to be done
        Hide
        Filip Procházka added a comment -

        Ofcourse I found it right after I wrote this one

        Show
        Filip Procházka added a comment - Ofcourse I found it right after I wrote this one
        Hide
        Filip Procházka added a comment -
        Show
        Filip Procházka added a comment - DDC-1149

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Filip Procházka
          • Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: