Skip to content

Loading…

DDC-891: DDC-117: No sequence generation with composite foreign key #5416

Open
doctrinebot opened this Issue · 4 comments

1 participant

@doctrinebot

Jira issue originally created by user felicitus:

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.

/****
 * @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;
    }
}
/****
 * @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;
    }
}
$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 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
@doctrinebot

Comment created by romanb:

I don't think this will ever be possible.

@doctrinebot

Comment created by felicitus:

Is there a technical reason for that? I mean, with DDC-117 we are aiming for composite foreign keys, or is DDC-117 cancelled?

@doctrinebot

Comment created by @beberlei:

A composite key is ALWAYS of the type "ASSIGNED" and cannot be a combination of different id generation strategies.

You could however write a prePersist Listener that does this for you.

@doctrinebot

Comment created by felicitus:

Okay, maybe this is a feature for 3.0 or so. However, I'd suggest leaving this bug open as this is something which needs to be documented once DDC-117 becomes integrated within the main branch.

Additionally, Doctrine should complain about different ID generation strategies. Right now it silently ignores it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.