DDC-137: Only last relation id updated with multiple one-to-one self-referencing relations #171

Closed
opened 2026-01-22 12:29:31 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 12, 2009).

Jira issue originally created by user reinier.kip:

Take this simplified situation:

/*** @Entity **/
class Entity {
    /*** @Id @GeneratedValue(strategy="AUTO") @Column(type="integer") **/
    public $id;

    /****
     * @OneToOne(targetEntity="Entity", cascade={"persist"})
     * @JoinColumn(name="other1", referencedColumnName="id")
     */
    public $other1;

    /****
     * @OneToOne(targetEntity="Entity", cascade={"persist"})
     * @JoinColumn(name="other2", referencedColumnName="id")
     */
    public $other2;
}

$entity1 = new Entity();
$em->persist($entity1);
$entity1->other1 = $entity2 = new Entity();
$entity1->other2 = $entity3 = new Entity();
$em->flush();

The entities' ids are now:
Entity 1: 1, Entity 2: 2, Entity 3: 3

However, the other1's relation id is not updated:

id  other1  other2
1   NULL    3:  // other1's id is missing
2   NULL    NULL
3   NULL    NULL

The SQL clarifies:

INSERT INTO Entity (other1, other2) VALUES (?, ?)
array(2) {
  [1]=>
  NULL
  [2]=>
  NULL
}
INSERT INTO Entity (other1, other2) VALUES (?, ?)
array(2) {
  [1]=>
  NULL
  [2]=>
  NULL
}
INSERT INTO Entity (other1, other2) VALUES (?, ?)
array(2) {
  [1]=>
  NULL
  [2]=>
  NULL
}
UPDATE Entity SET other2 = ? WHERE id = ?
array(2) {
  [0]=>
  int(3)
  [1]=>
  int(1)
}

Adding 'other3' proved that only the last relation id is updated, as such:

id  other1  other2  other3
1   NULL    NULL    4   // other1 and other2 missing
2   NULL    NULL    NULL
3   NULL    NULL    NULL
4   NULL    NULL    NULL

This is not the case with relations that reference to entities outside the class hierarchy (so the same problem occurs with relations inside the class hierarchy).

Originally created by @doctrinebot on GitHub (Nov 12, 2009). Jira issue originally created by user reinier.kip: Take this simplified situation: ``` /*** @Entity **/ class Entity { /*** @Id @GeneratedValue(strategy="AUTO") @Column(type="integer") **/ public $id; /**** * @OneToOne(targetEntity="Entity", cascade={"persist"}) * @JoinColumn(name="other1", referencedColumnName="id") */ public $other1; /**** * @OneToOne(targetEntity="Entity", cascade={"persist"}) * @JoinColumn(name="other2", referencedColumnName="id") */ public $other2; } $entity1 = new Entity(); $em->persist($entity1); $entity1->other1 = $entity2 = new Entity(); $entity1->other2 = $entity3 = new Entity(); $em->flush(); ``` The entities' ids are now: `Entity 1: 1, Entity 2: 2, Entity 3: 3` However, the other1's relation id is not updated: ``` id other1 other2 1 NULL 3: // other1's id is missing 2 NULL NULL 3 NULL NULL ``` The SQL clarifies: ``` INSERT INTO Entity (other1, other2) VALUES (?, ?) array(2) { [1]=> NULL [2]=> NULL } INSERT INTO Entity (other1, other2) VALUES (?, ?) array(2) { [1]=> NULL [2]=> NULL } INSERT INTO Entity (other1, other2) VALUES (?, ?) array(2) { [1]=> NULL [2]=> NULL } UPDATE Entity SET other2 = ? WHERE id = ? array(2) { [0]=> int(3) [1]=> int(1) } ``` Adding 'other3' proved that only the last relation id is updated, as such: ``` id other1 other2 other3 1 NULL NULL 4 // other1 and other2 missing 2 NULL NULL NULL 3 NULL NULL NULL 4 NULL NULL NULL ``` This is not the case with relations that reference to entities outside the class hierarchy (so the same problem occurs with relations inside the class hierarchy).
admin added the Bug label 2026-01-22 12:29:31 +01:00
admin closed this issue 2026-01-22 12:29:33 +01:00
Author
Owner

@doctrinebot commented on GitHub (Nov 15, 2009):

Comment created by romanb:

Thanks, should be fixed now.

@doctrinebot commented on GitHub (Nov 15, 2009): Comment created by romanb: Thanks, should be fixed now.
Author
Owner

@doctrinebot commented on GitHub (Nov 15, 2009):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Nov 15, 2009): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#171