Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-891

DDC-117: No sequence generation with composite foreign key

    Details

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

      Description

      Given the following entity definitions, Doctrine does not attempt to manage generated values. For example, in MySQL, it is perfectly possible to create a composite primary key and set auto_increment on one of these. See below the code for issues that occur.

      User.php
      /**
       * @Entity
       */
      class User {
      	/**
      	 * @Id
      	 * @Column(type="integer")
      	 * @GeneratedValue(strategy="AUTO")
      	 */
      	private $id;
      	
      	/**
      	 * @Column(type="string")
      	 */
      	private $name;
      	
      	/**
      	 * @OneToMany(targetEntity="PhoneNumber",mappedBy="id",cascade={"all"})
      	 */
      	private $phoneNumbers;
      	
      	public function setName ($name) {
      		$this->name = $name;
      	}
      }
      
      PhoneNumber.php
      /**
       * @Entity
       */
      class PhoneNumber {
      	/**
      	 * @Id
      	 * @Column(type="integer")
      	 * @GeneratedValue(strategy="AUTO")
      	 */
      	private $id;
      	
      	/**
      	 * @Id
      	 * @ManyToOne(targetEntity="User",cascade={"all"})
      	 */
      	private $user;
      	
      	/**
      	 * @Column(type="string")
      	 */
      	private $phonenumber;
      	
      	public function setUser (User $user) {
      		$this->user = $user;
      	}
      	
      	public function setPhoneNumber ($phoneNumber) {
      		$this->phonenumber = $phoneNumber;
      	}
      }
      
      Invokation
      $albert = new User;
      $albert->setName("albert");
      $em->persist($albert);
      
      $phoneAlbert1 = new PhoneNumber();
      $phoneAlbert1->setUser($albert);
      $phoneAlbert1->setPhoneNumber("albert home: 012345");
      $em->persist($phoneAlbert1);
      
      

      The first issue which occurs is that Doctrine does not generate the field "id" within PhoneNumber set to auto_increment.

      The second issue which occurs is that Doctrine becomes confused when inserting a new record into PhoneNumber, because of the following INSERT INTO statement:

      Insert Statement
      INSERT INTO PhoneNumber (user_id, phonenumber) VALUES (?, ?)
      array(1) {
        [1]=>
        string(19) "albert home: 012345"
      }
      
      SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
      

        Activity

        Timo A. Hummel created issue -
        Roman S. Borschel made changes -
        Field Original Value New Value
        Issue Type Bug [ 1 ] New Feature [ 2 ]
        Priority Major [ 3 ] Minor [ 4 ]
        Benjamin Eberlei made changes -
        Workflow jira [ 12158 ] jira-feedback [ 13884 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback [ 13884 ] jira-feedback2 [ 15748 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback2 [ 15748 ] jira-feedback3 [ 18005 ]

          People

          • Assignee:
            Roman S. Borschel
            Reporter:
            Timo A. Hummel
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: