3
votes

I would like to create 2 different resources App\Resource\Category et App\Resource\Classification, which would be associated to a same entity App\Entity\Classification. We have 2 different endpoints v1/classifications and v2/categories. I would have liked that both ressources extend the same entity.

Pseudo-code example:

<?php
// src/Entity/Classification.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ClassificationRepository")
 */
class Classification
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\ClassificationAttribute", mappedBy="classification")
     */
    private $classificationAttributes;

    public function __construct()
    {
        $this->classificationAttributes = new ArrayCollection();
    }

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

    // ... getter and setter methods
}
<?php
// src/Resource/Classification.php
namespace App\Resource;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Classification as ClassificationEntity;

/**
 * ...
 * @ApiResource(
 *     collectionOperations={"get"},
 *     itemOperations={"get"}
 * )
 */
class Classification extends ClassificationEntity
{
    // ...
}
<?php
// src/Resource/Category.php
namespace App\Resource;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Classification as ClassificationEntity;

/**
 * ...
 * @ApiResource(
 *     collectionOperations={"get"},
 *     itemOperations={"get"}
 * )
 */
class Category extends ClassificationEntity
{
    // ...
}

and each resource would have its endpoint, its serializer, its normaliser, etc, etc, ... To summarize, each resource would be customized its own way.

Except there is no possibility to use annotations et inheritance at the same time: annotations of the extended entity are not used.

I could associate a distinct DataProvider to each resource, and plug these DataProviders on the repository. But doing that, I lose every native fonctionality of the Doctrine ORM Extension and have to reimplement every one I use (an example among many: @ApiFilter)

Is there a simpler way to associate many resources to a same entity?

Thank you for your help.

1
You have 2 classes with the same name (Classification). - ehymel
These classes are not in the same namespace. I added it in the pseudo-code of the resources - danidan75
Judging by the code you'll need to look into Doctrine Inheritance Mapping - rkeet
If I did that, I would many entities extending the same mapped superclass entity. All the code would be in the mapped super class. The children would inherit, but with no specific code inside. It is not really a solution to the initial problem: one entity, many resources. - danidan75

1 Answers

0
votes

You need to add your Resource folder to the mapping paths in api/config/packages/api_platform.yaml:

api_platform:
    mapping:
        paths:
            - '%kernel.project_dir%/src/Entity'
            - '%kernel.project_dir%/src/Resource'

Resources that are not in these paths are ignored.