DDC-1407: Only owning side of x-1 associations can have a FK column? #1764

Closed
opened 2026-01-22 13:25:00 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 8, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user bhp:

I have to following tables:

TABLE1         TABLE2
------         ------
               id INT
id INT 1-----1 table1_id

My Entities:

class Table1
{
// ... //
/****
     * @var InversedEntity
     *
     * @ORM\OneToOne(targetEntity="Table2", cascade={"all"}, mappedBy="table1")
     */
    private $table2;
// ... //
}

class Table2
{
// ... //
/****
     * @var InversedEntity
     *
     * @ORM\OneToOne(targetEntity="Table1", cascade={"all"}, inversedBy="table2")
     */
    private $table1;
// ... //
}

If I set a id for TABLE1, while I do a $entity->flush() (with associated TABLE2), id in TABLE1 gets reset to null, because of this piece of code in BasicEntityPersister.php:

                foreach ($assoc['sourceToTargetKeyColumns'] as $sourceColumn => $targetColumn) {
                    if ($newVal === null) {
                        $result[$owningTable][$sourceColumn] = null;

Seems to me like the following assumption is wrong, if you ask me. Look at my table. This works on Hibernate...

                // Only owning side of x-1 associations can have a FK column.
                if ( ! $assoc['isOwningSide'] ](| ! ($assoc['type') & ClassMetadata::TO_ONE)) {
                    continue;
                }
Originally created by @doctrinebot on GitHub (Oct 8, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user bhp: **I have to following tables**: ``` TABLE1 TABLE2 ------ ------ id INT id INT 1-----1 table1_id ``` **My Entities**: ``` class Table1 { // ... // /**** * @var InversedEntity * * @ORM\OneToOne(targetEntity="Table2", cascade={"all"}, mappedBy="table1") */ private $table2; // ... // } class Table2 { // ... // /**** * @var InversedEntity * * @ORM\OneToOne(targetEntity="Table1", cascade={"all"}, inversedBy="table2") */ private $table1; // ... // } ``` **If I set a id for TABLE1, while I do a $entity->flush() (with associated TABLE2), id in TABLE1 gets reset to null, because of this piece of code in BasicEntityPersister.php**: ``` foreach ($assoc['sourceToTargetKeyColumns'] as $sourceColumn => $targetColumn) { if ($newVal === null) { $result[$owningTable][$sourceColumn] = null; ``` **Seems to me like the following assumption is wrong, if you ask me. Look at my table. This works on Hibernate...** ``` // Only owning side of x-1 associations can have a FK column. if ( ! $assoc['isOwningSide'] ](| ! ($assoc['type') & ClassMetadata::TO_ONE)) { continue; } ```
admin added the Bug label 2026-01-22 13:25:00 +01:00
admin closed this issue 2026-01-22 13:25:01 +01:00
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2011):

Comment created by @beberlei:

Will not work, because only the Inverse Side is set, but only the owning side is evaluated:

$a = new Table1();
$a->table2 = new Table2();

$em->persist($a);
$em->persist($a->table2);
$em->flush();

Will work, owning side isset:

$a = new Table1();
$a->table2 = new Table2();
$a->table2->table1 = $a;

$em->persist($a);
$em->persist($a->table2);
$em->flush();

This is the same behavior as in hibernate.

@doctrinebot commented on GitHub (Oct 15, 2011): Comment created by @beberlei: Will not work, because only the Inverse Side is set, but only the owning side is evaluated: ``` $a = new Table1(); $a->table2 = new Table2(); $em->persist($a); $em->persist($a->table2); $em->flush(); ``` Will work, owning side isset: ``` $a = new Table1(); $a->table2 = new Table2(); $a->table2->table1 = $a; $em->persist($a); $em->persist($a->table2); $em->flush(); ``` This is the same behavior as in hibernate.
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2011):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Oct 15, 2011): 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#1764