What is the reason why OneToOne relationship in Doctrine cannot be lazy loaded?









up vote
1
down vote

favorite












I have a Person and Admin entity. One Person can only have one Admin, but I do not need admin all the time. When I list all people with admin associated, doctrine will load all admins from DB. Why is this happening?










share|improve this question

























    up vote
    1
    down vote

    favorite












    I have a Person and Admin entity. One Person can only have one Admin, but I do not need admin all the time. When I list all people with admin associated, doctrine will load all admins from DB. Why is this happening?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a Person and Admin entity. One Person can only have one Admin, but I do not need admin all the time. When I list all people with admin associated, doctrine will load all admins from DB. Why is this happening?










      share|improve this question













      I have a Person and Admin entity. One Person can only have one Admin, but I do not need admin all the time. When I list all people with admin associated, doctrine will load all admins from DB. Why is this happening?







      doctrine2 lazy-loading one-to-one






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 2:42









      tom10271

      1,17731831




      1,17731831






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          In a case of one-to-one association (and in similar cases, e.g. while lazy loading one-to-many association) Doctrine generates so called proxy objects for associated entities. These proxy objects mimics interface of target entity but only triggers actual data loading from database upon access to non-id field.



          Because of this in your case when you're fetching list of Person entities - you doesn't get list of Admin entities fetched from database, but receiving list of Admin proxies instead. Unless you will (occasionally or intentionally) try to access some of properties of Admin entity (with exception of its id which can be safely accessed) Doctrine will not try to fetch any Admin information from database.



          Consider following simplified setup of A and B entities with one-to-one association:



          // A.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class A

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var B
          * @ORMOneToOne(targetEntity="AppEntityB")
          */
          private $b;

          /**
          * @return int
          */
          public function getId(): int

          return $this->id;


          /**
          * @return B
          */
          public function getB(): B

          return $this->b;



          // B.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class B

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var string


          You can proof that A::$b contain proxy object by either looking into debugger or by using reflection:



          $entities = $this->getEntityManager()->getRepository(AppEntityA::class)->findAll();
          /** @var AppEntityA $a */
          $a = array_shift($entities);
          $class = (new ReflectionObject($a->getB()))->getName();


          In this case value of $class variable will be Proxies__CG__AppEntityB (Doctrine proxy object for AppEntityB) and not AppEntityB as it would be in a case of normal entity object.






          share|improve this answer




















          • No my question is I am not accessing admin at all but it still got fetched
            – tom10271
            Nov 13 at 0:50










          • It is unlikely caused by Doctrine itself, it can be some part of your (or third-party) code that accesses proxied method, for example some kind of serializer or some reflection-based inspector. In this case I would recommend to set debugger breakpoint into proxy object for Admin entity and check where it was called from. Also looking into Doctrine SQL queries log may give you an idea how does information for these entities is being fetched.
            – Flying
            Nov 13 at 10:26










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255346%2fwhat-is-the-reason-why-onetoone-relationship-in-doctrine-cannot-be-lazy-loaded%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          In a case of one-to-one association (and in similar cases, e.g. while lazy loading one-to-many association) Doctrine generates so called proxy objects for associated entities. These proxy objects mimics interface of target entity but only triggers actual data loading from database upon access to non-id field.



          Because of this in your case when you're fetching list of Person entities - you doesn't get list of Admin entities fetched from database, but receiving list of Admin proxies instead. Unless you will (occasionally or intentionally) try to access some of properties of Admin entity (with exception of its id which can be safely accessed) Doctrine will not try to fetch any Admin information from database.



          Consider following simplified setup of A and B entities with one-to-one association:



          // A.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class A

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var B
          * @ORMOneToOne(targetEntity="AppEntityB")
          */
          private $b;

          /**
          * @return int
          */
          public function getId(): int

          return $this->id;


          /**
          * @return B
          */
          public function getB(): B

          return $this->b;



          // B.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class B

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var string


          You can proof that A::$b contain proxy object by either looking into debugger or by using reflection:



          $entities = $this->getEntityManager()->getRepository(AppEntityA::class)->findAll();
          /** @var AppEntityA $a */
          $a = array_shift($entities);
          $class = (new ReflectionObject($a->getB()))->getName();


          In this case value of $class variable will be Proxies__CG__AppEntityB (Doctrine proxy object for AppEntityB) and not AppEntityB as it would be in a case of normal entity object.






          share|improve this answer




















          • No my question is I am not accessing admin at all but it still got fetched
            – tom10271
            Nov 13 at 0:50










          • It is unlikely caused by Doctrine itself, it can be some part of your (or third-party) code that accesses proxied method, for example some kind of serializer or some reflection-based inspector. In this case I would recommend to set debugger breakpoint into proxy object for Admin entity and check where it was called from. Also looking into Doctrine SQL queries log may give you an idea how does information for these entities is being fetched.
            – Flying
            Nov 13 at 10:26














          up vote
          0
          down vote













          In a case of one-to-one association (and in similar cases, e.g. while lazy loading one-to-many association) Doctrine generates so called proxy objects for associated entities. These proxy objects mimics interface of target entity but only triggers actual data loading from database upon access to non-id field.



          Because of this in your case when you're fetching list of Person entities - you doesn't get list of Admin entities fetched from database, but receiving list of Admin proxies instead. Unless you will (occasionally or intentionally) try to access some of properties of Admin entity (with exception of its id which can be safely accessed) Doctrine will not try to fetch any Admin information from database.



          Consider following simplified setup of A and B entities with one-to-one association:



          // A.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class A

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var B
          * @ORMOneToOne(targetEntity="AppEntityB")
          */
          private $b;

          /**
          * @return int
          */
          public function getId(): int

          return $this->id;


          /**
          * @return B
          */
          public function getB(): B

          return $this->b;



          // B.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class B

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var string


          You can proof that A::$b contain proxy object by either looking into debugger or by using reflection:



          $entities = $this->getEntityManager()->getRepository(AppEntityA::class)->findAll();
          /** @var AppEntityA $a */
          $a = array_shift($entities);
          $class = (new ReflectionObject($a->getB()))->getName();


          In this case value of $class variable will be Proxies__CG__AppEntityB (Doctrine proxy object for AppEntityB) and not AppEntityB as it would be in a case of normal entity object.






          share|improve this answer




















          • No my question is I am not accessing admin at all but it still got fetched
            – tom10271
            Nov 13 at 0:50










          • It is unlikely caused by Doctrine itself, it can be some part of your (or third-party) code that accesses proxied method, for example some kind of serializer or some reflection-based inspector. In this case I would recommend to set debugger breakpoint into proxy object for Admin entity and check where it was called from. Also looking into Doctrine SQL queries log may give you an idea how does information for these entities is being fetched.
            – Flying
            Nov 13 at 10:26












          up vote
          0
          down vote










          up vote
          0
          down vote









          In a case of one-to-one association (and in similar cases, e.g. while lazy loading one-to-many association) Doctrine generates so called proxy objects for associated entities. These proxy objects mimics interface of target entity but only triggers actual data loading from database upon access to non-id field.



          Because of this in your case when you're fetching list of Person entities - you doesn't get list of Admin entities fetched from database, but receiving list of Admin proxies instead. Unless you will (occasionally or intentionally) try to access some of properties of Admin entity (with exception of its id which can be safely accessed) Doctrine will not try to fetch any Admin information from database.



          Consider following simplified setup of A and B entities with one-to-one association:



          // A.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class A

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var B
          * @ORMOneToOne(targetEntity="AppEntityB")
          */
          private $b;

          /**
          * @return int
          */
          public function getId(): int

          return $this->id;


          /**
          * @return B
          */
          public function getB(): B

          return $this->b;



          // B.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class B

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var string


          You can proof that A::$b contain proxy object by either looking into debugger or by using reflection:



          $entities = $this->getEntityManager()->getRepository(AppEntityA::class)->findAll();
          /** @var AppEntityA $a */
          $a = array_shift($entities);
          $class = (new ReflectionObject($a->getB()))->getName();


          In this case value of $class variable will be Proxies__CG__AppEntityB (Doctrine proxy object for AppEntityB) and not AppEntityB as it would be in a case of normal entity object.






          share|improve this answer












          In a case of one-to-one association (and in similar cases, e.g. while lazy loading one-to-many association) Doctrine generates so called proxy objects for associated entities. These proxy objects mimics interface of target entity but only triggers actual data loading from database upon access to non-id field.



          Because of this in your case when you're fetching list of Person entities - you doesn't get list of Admin entities fetched from database, but receiving list of Admin proxies instead. Unless you will (occasionally or intentionally) try to access some of properties of Admin entity (with exception of its id which can be safely accessed) Doctrine will not try to fetch any Admin information from database.



          Consider following simplified setup of A and B entities with one-to-one association:



          // A.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class A

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var B
          * @ORMOneToOne(targetEntity="AppEntityB")
          */
          private $b;

          /**
          * @return int
          */
          public function getId(): int

          return $this->id;


          /**
          * @return B
          */
          public function getB(): B

          return $this->b;



          // B.php

          <?php

          namespace AppEntity;

          use DoctrineORMMapping as ORM;

          /**
          * @ORMEntity()
          */
          class B

          /**
          * @var integer
          * @ORMColumn(type="integer")
          * @ORMId
          * @ORMGeneratedValue(strategy="AUTO")
          */
          private $id;
          /**
          * @var string


          You can proof that A::$b contain proxy object by either looking into debugger or by using reflection:



          $entities = $this->getEntityManager()->getRepository(AppEntityA::class)->findAll();
          /** @var AppEntityA $a */
          $a = array_shift($entities);
          $class = (new ReflectionObject($a->getB()))->getName();


          In this case value of $class variable will be Proxies__CG__AppEntityB (Doctrine proxy object for AppEntityB) and not AppEntityB as it would be in a case of normal entity object.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 16:29









          Flying

          2,5861718




          2,5861718











          • No my question is I am not accessing admin at all but it still got fetched
            – tom10271
            Nov 13 at 0:50










          • It is unlikely caused by Doctrine itself, it can be some part of your (or third-party) code that accesses proxied method, for example some kind of serializer or some reflection-based inspector. In this case I would recommend to set debugger breakpoint into proxy object for Admin entity and check where it was called from. Also looking into Doctrine SQL queries log may give you an idea how does information for these entities is being fetched.
            – Flying
            Nov 13 at 10:26
















          • No my question is I am not accessing admin at all but it still got fetched
            – tom10271
            Nov 13 at 0:50










          • It is unlikely caused by Doctrine itself, it can be some part of your (or third-party) code that accesses proxied method, for example some kind of serializer or some reflection-based inspector. In this case I would recommend to set debugger breakpoint into proxy object for Admin entity and check where it was called from. Also looking into Doctrine SQL queries log may give you an idea how does information for these entities is being fetched.
            – Flying
            Nov 13 at 10:26















          No my question is I am not accessing admin at all but it still got fetched
          – tom10271
          Nov 13 at 0:50




          No my question is I am not accessing admin at all but it still got fetched
          – tom10271
          Nov 13 at 0:50












          It is unlikely caused by Doctrine itself, it can be some part of your (or third-party) code that accesses proxied method, for example some kind of serializer or some reflection-based inspector. In this case I would recommend to set debugger breakpoint into proxy object for Admin entity and check where it was called from. Also looking into Doctrine SQL queries log may give you an idea how does information for these entities is being fetched.
          – Flying
          Nov 13 at 10:26




          It is unlikely caused by Doctrine itself, it can be some part of your (or third-party) code that accesses proxied method, for example some kind of serializer or some reflection-based inspector. In this case I would recommend to set debugger breakpoint into proxy object for Admin entity and check where it was called from. Also looking into Doctrine SQL queries log may give you an idea how does information for these entities is being fetched.
          – Flying
          Nov 13 at 10:26

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255346%2fwhat-is-the-reason-why-onetoone-relationship-in-doctrine-cannot-be-lazy-loaded%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto