Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1121

Doctrine CLI tool produces wrong schema when the id columns of the entities are not named as $id. The annotations block aren't taken into account

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 2.0.4
    • Fix Version/s: 2.0.5
    • Component/s: Tools
    • Security Level: All
    • Labels:
      None
    • Environment:
      Windows xp sp3, php 5.3.6, apache 2.2.17

      Description

      namespace Entities;
      
      //use Doctrine\Common\Collections\ArrayCollection;
      
      /** 
       *@Entity
       *@Table(name="user")
       */
      class User
      {
      	/** 
      	*@Id 
      	*@Column(type="integer", name="user_id")
      	*@GeneratedValue
      	* */
          private $userId;
      	
      	/** @Column(length=255) */
          private $lastName;
      	
      	/** @Column(length=255, name="user_firstname") */
          private $firstName;
      	
      	 /**
           * Bidirectional - Many users are geathered within one group (OWNING SIDE)
           *
           * @ManyToOne(targetEntity="\Entities\Group", inversedBy="user")
           */
      	private $group;
      	
      /// ---
      }
      
      <?php
      namespace Entities;
      /** 
       *@Entity
       *@Table(name="group")
       */
      class Group
      {
      	/** 
      	*@Id 
      	*@Column(type="integer", name="group_id")
      	*@GeneratedValue
      	* */
          private $groupId;
      	
      	/** @Column(name="group_name") */
          private $name;
      	
          /**
           * @OneToMany(targetEntity="\Entities\User", mappedBy="group")
           */
          private $users;
      
          public function __construct() {
              $this->users = new \Doctrine\Common\Collections\ArrayCollection();
          }
      }
      

      In the following examples the id fields are set to $userId (mapped to user_id) and $groupId (mapped to group_id), but when it comes to generating the schema an error occures. The Doctrine CLI Tool doesn't work properly. When it generates the schema it fails to take into account the annotation blocks and just uses "id" instead of "group_id".

      Let me explain it better. If we run the tool it will produce the following code

      ...

      ALTER TABLE user ADD FOREIGN KEY (group_id) REFERENCES group(id)
      

      instead of

      ALTER TABLE `user`  ADD  FOREIGN KEY (`group_id`) REFERENCES `groups` (`group_id`);
      

      Moreover using "group" as a name of the entity class will lead to numerous problems, because it's a reserved word in SQL. Even if we specify the quoatation forcibly using backticks it won't work:

       /**@Table(name="`group`")*/
      Class Group {
      

      The quoatation won't be taken into account while generation the schema.

      I'm enclosing the files I've tried to play with. Just try to generate the schema.

        Activity

        Hide
        Benjamin Eberlei added a comment -

        Fixed formatting, scheduled for 2.0.5

        Show
        Benjamin Eberlei added a comment - Fixed formatting, scheduled for 2.0.5
        Hide
        Benjamin Eberlei added a comment -

        Just understood this. This is expected behavior, "id" is the default value. Doctrine cannot - for optimization reasons - check the id column names on the other entity. That is what the @JoinColumn annotation is for, where you can explicitly give a name.

        Show
        Benjamin Eberlei added a comment - Just understood this. This is expected behavior, "id" is the default value. Doctrine cannot - for optimization reasons - check the id column names on the other entity. That is what the @JoinColumn annotation is for, where you can explicitly give a name.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Eddie O'Donnell
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: