Problem with api-plateform when using 2 level of inheritance with doctrine entity

Viewed 122

I have a problem in a 2 level of inheritance structure with api-platfom but maybe with doctrine too

I have two question about this code :

  • doctrine is well generating a discr column in the MyParent class, but not in MyChild class for some reason... I don't think it is normal as how to know that an instance is MyChildChild if there is no discr column in MyChild ?
  • with api-platform, I have some errors as soon as I have 2 levels of inheritance. As soon as I remove one of the "extends MyChild" or "extends MyParent" api-platform behaviour is ok but loose the fields of the removed class (normal)

Is there something I'm doing wrong ? I have already use multiple level of inheritance with doctrine and I don't remember that I had this behaviour (only one discr column for 2 classes). And for api-platform, I'm just discovering so maybe I missed something

Thank you in advance

RV

Here is my code

    // src/Entity/MyParent.php
    namespace App\Entity;
    
    use ApiPlatform\Core\Annotation\ApiResource;
    use App\Repository\MyParentRepository;
    use Doctrine\ORM\Mapping as ORM;
    use App\Entity\MyChild;
    
    /**
     * @ApiResource()
     * @ORM\Entity(repositoryClass=MyParentRepository::class)
     * @ORM\InheritanceType("JOINED")
     * @ORM\DiscriminatorColumn(name="discr", type="string")
     * @ORM\DiscriminatorMap({"child"="MyChild"})
     */
    class MyParent
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=255)
         */
        private $parentAttribute;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        public function getParentAttribute(): ?string
        {
            return $this->parentAttribute;
        }
    
        public function setParentAttribute(string $parentAttribute): self
        {
            $this->parentAttribute = $parentAttribute;
    
            return $this;
        }
    }

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\MyChildRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\MyChildChild;
use App\Entity\MyParent;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=MyChildRepository::class)
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"childchild"="MyChildChild"})
 */
class MyChild extends MyParent
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $childAttribute;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getChildAttribute(): ?string
    {
        return $this->childAttribute;
    }

    public function setChildAttribute(string $childAttribute): self
    {
        $this->childAttribute = $childAttribute;

        return $this;
    }
}

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\MyChildChildRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\MyChild;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=MyChildChildRepository::class)
 */
class MyChildChild extends MyChild
{

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $MyChildChildAttribute;

    public function getMyChildChildAttribute(): ?string
    {
        return $this->MyChildChildAttribute;
    }

    public function setMyChildChildAttribute(string $MyChildChildAttribute): self
    {
        $this->MyChildChildAttribute = $MyChildChildAttribute;

        return $this;
    }
}
0 Answers
Related