0

Summary: Doctrine Embedded types are failing to be populated when they are contained in entities which are themselves contained in another top-level entity.

Details: I have a top-level entity A which has a ManyToOne relationship with an intermediate-level entity B, which in turn has an Embedded object C.

When I retrieve the top-level entity (using $entityManager->getRepository(A::class)->find($aid) ), the returned A object's $b variable has its $c property explicitly set to null (rather than - as you might expect - being absent altogether). Querying the $c property does not cause B to hydrate - instead $c remains null.

If I first put a dummy call into to get a simple property of B e.g. reference, then B hydrates correctly and $c is populated correctly.

Similarly, if I come in at the intermediate level with $entityManager->getRepository(B::class)->find($bid) then there is no problem either.

Is this a limitation in Doctrine?

(A possible complicating factor is the fact that A itself is a base class for 2 further entities)

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="type", type="string")
 * @DiscriminatorMap({"child1" = "Child1 "child2" = "Child2"})
 * @Table(name="A")
 */
class A
{
    /**
     * @ManyToOne(targetEntity="Model\B", inversedBy="As")
     * @JoinColumn(name="b_id", referencedColumnName="id",)
     * @var B
     */
    public $b;
}

/**
 * @Entity
 * @Table(name="b")
 */
class B
{
    /**
     * @Embedded(class="Model\C", columnPrefix = false)
     * @var C
     */
    public $c;

    /**
     * @Column(type="string")
     * @var string
     */
    public $reference;
}

/**
 * @Embeddable
 */
class C
{
    /**
     * @Column(type="smallint")
     * @var int
     */
    public $type;   
}
Russ Schultz
  • 2,545
  • 20
  • 22
Hadden Uff
  • 99
  • 2
  • 10
  • Doctrine will hydrate the other classes only when you do something to access data in one of those other classes. Its supposed to stop it getting data because of relationships unnecessarily. So it only hydrates as yo attempt to access properties of those other classes – RiggsFolly Jul 18 '18 at 13:34
  • @RiggsFolly the point is that it's NOT hydrating as I attempt to access the embedded property – Hadden Uff Jul 18 '18 at 13:35
  • Ok, I will dissapear then :) – RiggsFolly Jul 18 '18 at 13:37
  • 1
    @HaddenUff the issue might be connected with fact that you're using `public` visibility for properties and there might be some problem with `Proxy` initializing field with embeddable. I'd try to see if the issue persists with `private` properties. If it works, it'd be good to submit a bug report. – malarzm Jul 18 '18 at 19:04
  • Spot on @malarzm. Changing `public $c;` to `private $c;` and providing an accessor avoids the error. Thanks very much for your help, I'll submit a bug report as you suggest – Hadden Uff Jul 23 '18 at 19:17

0 Answers0