Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1970

DiscriminatorMap recursion when using self-reference

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.3
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      I've ran into a problem with self-referencing entity. When fetching an entity, recursion occurs, fetching every related entity defined by ManyToOne relation
      (in this example $sponsor), ignoring LAZY or EXTRA_LAZY fetch mode - it executes numerous queries.

      /**
       * @ORM\Entity(repositoryClass="Acme\Bundle\UserBundle\Entity\Repository\UserRepository")
       * @ORM\Table(name="f_user")
       * @ORM\InheritanceType("JOINED")
       * @ORM\DiscriminatorColumn(name="type", type="string")
       * @ORM\DiscriminatorMap({"user_person" = "UserPerson", "user_company" = "UserCompany"})
       */
      abstract class UserBase extends FOSUser
      
      /* .... */
      
          /**
           * @var UserBase
           *
           * @ORM\OneToMany(targetEntity="UserBase", mappedBy="sponsor")
           */
          protected $referrals;
      
          /**
           * @ORM\ManyToOne(targetEntity="UserBase", inversedBy="referrals")
           * @ORM\JoinColumn(name="sponsor_id", referencedColumnName="id")
           */
          protected $sponsor;
      
      

        Activity

        Krzysztof Kolasiak created issue -
        Krzysztof Kolasiak made changes -
        Field Original Value New Value
        Description I've ran into a problem with self-referencing entity. When fetching an entity, recursion occurs, fetching every related entity defined by ManyToOne relation.
        (in this example $sponsor), ignoring LAZY or EXTRA_LAZY fetch mode.

        {code}
        /**
         * @ORM\Entity(repositoryClass="Acme\Bundle\UserBundle\Entity\Repository\UserRepository")
         * @ORM\Table(name="f_user")
         * @ORM\InheritanceType("JOINED")
         * @ORM\DiscriminatorColumn(name="type", type="string")
         * @ORM\DiscriminatorMap({"user_person" = "UserPerson", "user_company" = "UserCompany"})
         */
        abstract class UserBase extends FOSUser

        /* .... */

            /**
             * @var UserBase
             *
             * @ORM\OneToMany(targetEntity="UserBase", mappedBy="sponsor")
             */
            protected $referrals;

            /**
             * @ORM\ManyToOne(targetEntity="UserBase", inversedBy="referrals")
             * @ORM\JoinColumn(name="sponsor_id", referencedColumnName="id")
             */
            protected $sponsor;

        {/code}
        I've ran into a problem with self-referencing entity. When fetching an entity, recursion occurs, fetching every related entity defined by ManyToOne relation.
        (in this example $sponsor), ignoring LAZY or EXTRA_LAZY fetch mode.

        {code}
        /**
         * @ORM\Entity(repositoryClass="Acme\Bundle\UserBundle\Entity\Repository\UserRepository")
         * @ORM\Table(name="f_user")
         * @ORM\InheritanceType("JOINED")
         * @ORM\DiscriminatorColumn(name="type", type="string")
         * @ORM\DiscriminatorMap({"user_person" = "UserPerson", "user_company" = "UserCompany"})
         */
        abstract class UserBase extends FOSUser

        /* .... */

            /**
             * @var UserBase
             *
             * @ORM\OneToMany(targetEntity="UserBase", mappedBy="sponsor")
             */
            protected $referrals;

            /**
             * @ORM\ManyToOne(targetEntity="UserBase", inversedBy="referrals")
             * @ORM\JoinColumn(name="sponsor_id", referencedColumnName="id")
             */
            protected $sponsor;

        {code}
        Krzysztof Kolasiak made changes -
        Description I've ran into a problem with self-referencing entity. When fetching an entity, recursion occurs, fetching every related entity defined by ManyToOne relation.
        (in this example $sponsor), ignoring LAZY or EXTRA_LAZY fetch mode.

        {code}
        /**
         * @ORM\Entity(repositoryClass="Acme\Bundle\UserBundle\Entity\Repository\UserRepository")
         * @ORM\Table(name="f_user")
         * @ORM\InheritanceType("JOINED")
         * @ORM\DiscriminatorColumn(name="type", type="string")
         * @ORM\DiscriminatorMap({"user_person" = "UserPerson", "user_company" = "UserCompany"})
         */
        abstract class UserBase extends FOSUser

        /* .... */

            /**
             * @var UserBase
             *
             * @ORM\OneToMany(targetEntity="UserBase", mappedBy="sponsor")
             */
            protected $referrals;

            /**
             * @ORM\ManyToOne(targetEntity="UserBase", inversedBy="referrals")
             * @ORM\JoinColumn(name="sponsor_id", referencedColumnName="id")
             */
            protected $sponsor;

        {code}
        I've ran into a problem with self-referencing entity. When fetching an entity, recursion occurs, fetching every related entity defined by ManyToOne relation
        (in this example $sponsor), ignoring LAZY or EXTRA_LAZY fetch mode - it executes numerous queries.

        {code}
        /**
         * @ORM\Entity(repositoryClass="Acme\Bundle\UserBundle\Entity\Repository\UserRepository")
         * @ORM\Table(name="f_user")
         * @ORM\InheritanceType("JOINED")
         * @ORM\DiscriminatorColumn(name="type", type="string")
         * @ORM\DiscriminatorMap({"user_person" = "UserPerson", "user_company" = "UserCompany"})
         */
        abstract class UserBase extends FOSUser

        /* .... */

            /**
             * @var UserBase
             *
             * @ORM\OneToMany(targetEntity="UserBase", mappedBy="sponsor")
             */
            protected $referrals;

            /**
             * @ORM\ManyToOne(targetEntity="UserBase", inversedBy="referrals")
             * @ORM\JoinColumn(name="sponsor_id", referencedColumnName="id")
             */
            protected $sponsor;

        {code}
        Alexander made changes -
        Issue Type Bug [ 1 ] New Feature [ 2 ]
        Marco Pivetta made changes -
        Priority Critical [ 2 ] Major [ 3 ]

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Krzysztof Kolasiak
          • Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated: