core: Wrong attributes serialized when embedding multiple different inherited entities

When serializing an related entity which hast some kind of entity with inheritance embedded (doesn’t actually matter if it’s single table or joined). The properties of the parent class are serialized, and ONLY the properties of one of the child classes. But for all entites, regardless of their type.

This is a sample setup:


/**
 * @ApiResource()
 * @ORM\Entity
 * @ORM\InheritanceType(value="JOINED")
 */
class ParentClass
{
    /**
     * @var int The entity Id
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"get"})
     */
    protected $id;

    /**
     * @var string Something else
     *
     * @ORM\Column
     * @Assert\NotBlank
     * @Groups({"get"})
     */
    protected $baz = '';
}
/**
 * @ApiResource
 * @ORM\Entity
 */
class BarChild extends ParentClass
{
    /**
     * @var int The entity Id
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @var string Something else
     *
     * @ORM\Column
     * @Assert\NotBlank
     * @Groups({"get"})
     */
    private $bar = '';
}
/**
 * @ApiResource
 * @ORM\Entity
 */
class FooChild extends ParentClass
{
    /**
     * @var int The entity Id
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @var string Something else
     *
     * @ORM\Column
     * @Assert\NotBlank
     * @Groups({"get"})
     */
    private $foo = '';
/**
 * @ApiResource(attributes={"normalization_context"={"groups"={"get"}}})
 * @ORM\Entity()
 */
class RelatedEntity
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @Groups({"get"})
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     * @Groups({"get"})
     */
    private $attribute;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\ParentClass", mappedBy="relatedEntity")
     * @Groups({"get"})
     */
    private $relatedEntites;
}

When retrieving a RelatedEntity which has a relation to one FooChild and one BarChild the result looks like this:

{
  "@context": "\/app_dev.php\/contexts\/RelatedEntity",
  "@id": "\/app_dev.php\/related_entities\/1",
  "@type": "RelatedEntity",
  "id": 1,
  "attribute": "test",
  "relatedEntites": [
    {
      "@id": "\/app_dev.php\/foo_children\/1",
      "@type": "FooChild",
      "id": 1,
      "baz": "test2",
      "bar": null
    },
    {
      "@id": "\/app_dev.php\/bar_children\/2",
      "@type": "BarChild",
      "id": 2,
      "baz": "testbaz",
      "bar": ""
    }
  ]
}

But the FooChild Entity should not have the property bar, but foo instead. And in my testing date the value of baron the BarChild entity is actually not an empty string…

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

While building my data model I didn’t feel very good about it either. But actually I can think of some cases where you need inheritance for entities. For example https://symfony.com/legacy/doc/more-with-symfony/1_4/en/09-Doctrine-Form-Inheritance I understand that this is legacy documentation, but the cases at the beginning seem valid to me…

Or in my project I have an entity that consists of many different components in one Collection. These componentes can be of different type and thus have different properties. It wouldn’t make much sense to only serialize the shared properties and the parent properties are serialized, like @soyuka suggests.

But I am open for suggestions how I can solve this problem without inheritance. Perhaps I started wrong from the beginning on.

The work in progress PR is coming tomorrow morning CET.