Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2421

Many-To-Many relation creation failed when using non PK entity field

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: Git Master, 2.3.3
    • Fix Version/s: None
    • Component/s: Mapping Drivers, Tools
    • Security Level: All
    • Labels:
    • Environment:
      Ubuntu linux 12.04, php 5.4.9

      Description

      Given these entities :

      /**
       * Class Domain
       *
       * @ORM\Entity
       * @ORM\Table(name="profils_domains")
       */
      class Domain
      {
          /**
           * @var string
           *
           * @ORM\Id
           * @ORM\Column(type="string", length=22, nullable=false)
           */
          protected $name = '';
      }
      
      /**
       * Class Web
       *
       * @ORM\Entity
       * @ORM\Table(name="profils_webs",
       *          uniqueConstraints={@ORM\UniqueConstraint(name="web_unique",columns={"name", "domain"})}
       * )
       */
      class Web
      {
          /**
           * @var integer
           *
           * @ORM\Id
           * @ORM\GeneratedValue
           * @ORM\Column(type="integer", nullable=false)
           */
          protected $id;
      
          /**
           * @var string
           *
           * @ORM\Column(type="string", length=22, nullable=false)
           */
          protected $name = '';
      
          /**
           * @var Domain
           *
           * @ORM\ManyToOne(targetEntity="Domain", fetch="LAZY")
           * @ORM\JoinColumn(name="domain", referencedColumnName="name", nullable=false, onDelete="CASCADE")
           */
          protected $domain;
      }
      
      /**
       * Class WebsGroup
       *
       * @ORM\Entity
       * @ORM\Table(name="profils_websgroups",
       *          uniqueConstraints={@ORM\UniqueConstraint(name="websgroup_unique",columns={"name", "domain"})}
       * )
       */
      class WebsGroup
      {
          /**
           * @var integer
           *
           * @ORM\Id
           * @ORM\GeneratedValue
           * @ORM\Column(type="integer", nullable=false)
           */
          protected $id;
      
          /**
           * @var string
           *
           * @ORM\Column(type="string", length=22, nullable=false)
           */
          protected $name = '';
      
          /**
           * @var Domain
           *
           * @ORM\ManyToOne(targetEntity="Domain", fetch="LAZY")
           * @ORM\JoinColumn(name="domain", referencedColumnName="name", nullable=false, onDelete="CASCADE")
           */
          protected $domain;
      
          /**
           * @var ArrayCollection
           *
           * @ORM\ManyToMany(targetEntity="Web", indexBy="id", fetch="EXTRA_LAZY")
           * @ORM\JoinTable(name="profils_websgroups_webs",
           *      joinColumns={
           * @ORM\JoinColumn(name="websgroup_id", referencedColumnName="id", onDelete="CASCADE"),
           * @ORM\JoinColumn(name="domain", referencedColumnName="domain", onDelete="CASCADE")
           *          },
           *      inverseJoinColumns={
           * @ORM\JoinColumn(name="web_id", referencedColumnName="id", onDelete="CASCADE"),
           * @ORM\JoinColumn(name="domain", referencedColumnName="domain", onDelete="CASCADE")
           *          }
           *      )
           */
          protected $webs;
      }
      

      I've got a domain, some web sites per domain and websgroups which group web sites. I want to be sure in my database that a web group from a domain D can contain only web sites from the very same domain but when calling the console tool for creating my schema it raise :

      [Doctrine\ORM\ORMException]                                                                                                                                      
        Column name `domain` referenced for relation from Entity\WebsGroup towards Entity\Web does not exist. 
      

      It's because domain is already an association to an entity which and is not part of the primary key.

      I've quick fixed getDefiningClass from Doctrine\ORM\Tools\SchemaTool to make it work but i really don't know if it's the proper way :

          private function getDefiningClass($class, $referencedColumnName)
          {
              $referencedFieldName = $class->getFieldName($referencedColumnName);
      
              if ($class->hasField($referencedFieldName)) {
                  return array($class, $referencedFieldName);
              } else if (in_array($referencedColumnName, $class->getIdentifierColumnNames())) {
                  // it seems to be an entity as foreign key
                  foreach ($class->getIdentifierFieldNames() as $fieldName) {
                      if ($class->hasAssociation($fieldName) && $class->getSingleAssociationJoinColumnName($fieldName) == $referencedColumnName) {
                          return $this->getDefiningClass(
                              $this->em->getClassMetadata($class->associationMappings[$fieldName]['targetEntity']),
                              $class->getSingleAssociationReferencedJoinColumnName($fieldName)
                          );
                      }
                  }
              } else if (in_array($referencedColumnName, $class->getAssociationNames())) {
                  return $this->getDefiningClass(
                      $this->em->getClassMetadata($class->associationMappings[$referencedColumnName]['targetEntity']),
                      $class->getSingleAssociationReferencedJoinColumnName($referencedColumnName)
                  );
              }
      
              return null;
          }
      

        Activity

        Bruno CHALOPIN created issue -
        Benjamin Eberlei made changes -
        Field Original Value New Value
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Invalid [ 6 ]

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Bruno CHALOPIN
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: