Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-357

Lazy-loading in OneToOne-bidirectional associations not working for inverse side

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Won't Fix
    • Affects Version/s: 2.0-ALPHA4
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      XAMPP 1.7.3

      Description

      I am referring to the following situation:
      http://www.doctrine-project.org/documentation/manual/2_0/en/association-mapping#one-to-one,-bidirectional

      In this example: if I fetch an object of type "Customer" from my database, a second query is executed immediately that fetches the corresponding "Cart" even if I do not access the $cart property of "Customer". Annotating fetch="LAZY" to the $cart property does not make any difference. This is even worse in case of self-referencing objects, e.g. those having at most one parent object and at most one child object. Here, all associations are created by single database queries at once (e.g. fetching the child object, then the child of the child object and so forth).

      By contrast, OneToMany associations are lazy-loaded from the inverse side (as expected).

      Perhaps I should add, that I am using annotation mappings for my entities (no YAML, no XML).

        Issue Links

          Activity

          Hide
          Filip Procházka added a comment -

          I'm proposing a solution to this "Won't Fix" that I disagree with https://github.com/doctrine/doctrine2/pull/970

          Show
          Filip Procházka added a comment - I'm proposing a solution to this "Won't Fix" that I disagree with https://github.com/doctrine/doctrine2/pull/970
          Hide
          Doctrine Bot added a comment -

          A related Github Pull-Request [GH-970] was closed:
          https://github.com/doctrine/doctrine2/pull/970

          Show
          Doctrine Bot added a comment - A related Github Pull-Request [GH-970] was closed: https://github.com/doctrine/doctrine2/pull/970
          Hide
          Misha Bosiy added a comment -

          My solution:
          Two entities with one-to-one reference Site and Url:

          *********************************

          Site

          /**

          • @var Url
            *
          • @ORM\OneToOne(targetEntity="Url", cascade= {"persist"}

            )

          • @ORM\JoinColumns( { * @ORM\JoinColumn(name="url_id", referencedColumnName="url_id") * }

            )
            */
            private $url;

          /**

          • @return Url
            */
            public function getUrl() { return $this->url; }

          *********************************

          Url

          /**

          • @var \Doctrine\ORM\PersistentCollection
            *
          • @ORM\OneToMany(targetEntity="Site", mappedBy="url")
            */
            private $site;

          /**

          • @return Site|null
            */
            public function getSite() { return ($this->site->first() !== false) ? $this->site->first() : null; }

          *********************************

          Queries:

          $url = $urlRepo->createQueryBuilder('u')
          >where('u.url_id = :url_id')>setParameter('url_id', 19518)
          ->getQuery()
          ->getOneOrNullResult();

          We have only 1 query to DB.

          When call:
          $url->getSite();

          One more query appears. So lazy load work perfect!

          ********************************

          If with join:
          $url = $urlRepo->createQueryBuilder('u')
          ->select('u', 's')
          ->join('u.site', 's')
          >where('u.url_id = :url_id')>setParameter('url_id', 19518)
          ->getQuery()
          ->getOneOrNullResult();

          When call:
          $url->getSite();

          Only 1 query to DB.

          Show
          Misha Bosiy added a comment - My solution: Two entities with one-to-one reference Site and Url: ********************************* Site /** @var Url * @ORM\OneToOne(targetEntity="Url", cascade= {"persist"} ) @ORM\JoinColumns( { * @ORM\JoinColumn(name="url_id", referencedColumnName="url_id") * } ) */ private $url; /** @return Url */ public function getUrl() { return $this->url; } ********************************* Url /** @var \Doctrine\ORM\PersistentCollection * @ORM\OneToMany(targetEntity="Site", mappedBy="url") */ private $site; /** @return Site|null */ public function getSite() { return ($this->site->first() !== false) ? $this->site->first() : null; } ********************************* Queries: $url = $urlRepo->createQueryBuilder('u') >where('u.url_id = :url_id') >setParameter('url_id', 19518) ->getQuery() ->getOneOrNullResult(); We have only 1 query to DB. When call: $url->getSite(); One more query appears. So lazy load work perfect! ******************************** If with join: $url = $urlRepo->createQueryBuilder('u') ->select('u', 's') ->join('u.site', 's') >where('u.url_id = :url_id') >setParameter('url_id', 19518) ->getQuery() ->getOneOrNullResult(); When call: $url->getSite(); Only 1 query to DB.
          Hide
          Filip Procházka added a comment -

          That is not a solution but a workaround which we are also using.

          Show
          Filip Procházka added a comment - That is not a solution but a workaround which we are also using.
          Hide
          Misha Bosiy added a comment -

          I wrote it for people who has such problem and don't know what to do with not necessary subqueries.

          Show
          Misha Bosiy added a comment - I wrote it for people who has such problem and don't know what to do with not necessary subqueries.

            People

            • Assignee:
              Benjamin Eberlei
              Reporter:
              Marcel Walter
            • Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: