DDC-2983: TCI association not getting hydrated into sql statement #3711

Open
opened 2026-01-22 14:25:54 +01:00 by admin · 1 comment
Owner

Originally created by @doctrinebot on GitHub (Feb 14, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user machete:


/****
 * @ORM\Entity
 * @ORM\Table(name="base")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discriminator", type="string")
 * @ORM\DiscriminatorMap({
 * "a" = "A",
 * "b" = "B",
 * })
 */
class Base
{
    /****
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

/****
 * @ORM\Entity
****/
class A extends Base {
    /****
     * @ORM\ManyToOne(targetEntity="B", inversedBy="as")
     */
    protected $b;

    /****
     * @ORM\OneToMany(targetEntity="C", cascade={"persist"})
     */
    protected $cs;
}

/****
 * @ORM\Entity
****/
class B Extends Base {
    /****
     * @ORM\OneToMany(targetEntity="A", mappedBy="b")
     */
    protected $as;
}

/****
 * @ORM\Entity
****/
class C {
  // ...
}

Doing this:

$a = new A();
$b = new B();

$a->setB($b);
$b->addA($a);

var_dump($a->getB()); // outputs B (b not null!)
var*dump($b->getAs()) // outputs A (private $*elements => [0] class B)

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

Leads to

integrity constraint violation, b_id can not be null

aka

INSERT INTO a (id, b_id), (123, null);

I have no idea why this happens. This works:

$a = new A();
$b = new B();

$a->setB($b);
$b->addA($a);

var_dump($a->getB()); // outputs B (b not null!)
var*dump($b->getAs()) // outputs A (private $*elements => [0] class B)

$em->persist($a);
$em->flush();
$em->persist($b);
$em->flush();
Originally created by @doctrinebot on GitHub (Feb 14, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user machete: ``` /**** * @ORM\Entity * @ORM\Table(name="base") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="discriminator", type="string") * @ORM\DiscriminatorMap({ * "a" = "A", * "b" = "B", * }) */ class Base { /**** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; } /**** * @ORM\Entity ****/ class A extends Base { /**** * @ORM\ManyToOne(targetEntity="B", inversedBy="as") */ protected $b; /**** * @ORM\OneToMany(targetEntity="C", cascade={"persist"}) */ protected $cs; } /**** * @ORM\Entity ****/ class B Extends Base { /**** * @ORM\OneToMany(targetEntity="A", mappedBy="b") */ protected $as; } /**** * @ORM\Entity ****/ class C { // ... } ``` Doing this: ``` $a = new A(); $b = new B(); $a->setB($b); $b->addA($a); var_dump($a->getB()); // outputs B (b not null!) var*dump($b->getAs()) // outputs A (private $*elements => [0] class B) $em->persist($a); $em->persist($b); $em->flush(); ``` Leads to integrity constraint violation, b_id can not be null aka INSERT INTO a (id, b_id), (123, null); I have no idea why this happens. This works: ``` $a = new A(); $b = new B(); $a->setB($b); $b->addA($a); var_dump($a->getB()); // outputs B (b not null!) var*dump($b->getAs()) // outputs A (private $*elements => [0] class B) $em->persist($a); $em->flush(); $em->persist($b); $em->flush(); ```
admin added the Bug label 2026-01-22 14:25:54 +01:00
Author
Owner

@doctrinebot commented on GitHub (Mar 23, 2014):

Comment created by @beberlei:

This most likely happens because the UnitOfWork tries to set the owner and reverse incorrectly due to an ordering issue.

@doctrinebot commented on GitHub (Mar 23, 2014): Comment created by @beberlei: This most likely happens because the UnitOfWork tries to set the owner and reverse incorrectly due to an ordering issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3711