如何扩展学说实体以与 API 平台一起使用

How to extend the doctrine entity to be working with API platform

我有一个案例,我想用 API 平台注释分离学说实体。 主要问题是我能够创建一个资源,它看起来没问题,但它没有保存在数据库中。看来我还需要补充一件事,但不知道该放什么。

映射正确,数据库是最新的...

实体class:

<?php
declare(strict_types=1);

namespace Entity;

use Ramsey\Uuid\UuidInterface;

class Example
{
    /**
     * @var UuidInterface
     */
    protected $uuid;

    /**
     * @var string
     */
    protected $name;
}

实体映射:

<doctrine-mapping
    xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"
>
    <entity name="Entity\Example" table="examples">
        <id name="uuid" type="uuid" column="uuid" />
        <field name="name" type="string" column="name" />
    </entity>
</doctrine-mapping>

Api 平台 class

<?php
declare(strict_types=1);

namespace ApiPlatform;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Entity\Example as EntityExample;
use Ramsey\Uuid\UuidInterface;

/**
 * @ApiResource()
 */
class Example extends EntityExample
{
    /**
     * @ApiProperty(identifier=true)
     * @var UuidInterface
     */
    protected $uuid;

    /**
     * @return UuidInterface
     */
    public function getUuid() : UuidInterface
    {
        return $this->uuid;
    }

    /**
     * @return string
     */
    public function getName() : string
    {
        return $this->name;
    }

    /**
     * @param UuidInterface $uuid
     */
    public function setUuid(UuidInterface $uuid) : void
    {
        $this->uuid = $uuid;
    }

    /**
     * @param string $name
     */
    public function setName(string $name) : void
    {
        $this->name = $name;
    }
}

首先,您的示例 class 缺少 $name 属性。 在 $uuid 之后添加 private $name;,除非 EntityExample 具有 protected/public 属性.

您的 ApiResource() 注释 class 似乎没有针对任何属性的序列化组注释。 https://api-platform.com/docs/core/serialization/

<?php
declare(strict_types=1);

namespace ApiPlatform;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Entity\Example as EntityExample;
use Ramsey\Uuid\UuidInterface;

/**
  * @ApiResource(
  *     normalizationContext={"groups"={"read"}},
  *     denormalizationContext={"groups"={"write"}}
  * )
 */
class Example extends EntityExample
{
    /**
     * @ApiProperty(identifier=true)
     * @var UuidInterface
     */
    protected $uuid;

    /**
     * @Groups({"read", "write"})
     */
    private $name;

    /**
     * @return UuidInterface
     */
    public function getUuid() : UuidInterface
    {
        return $this->uuid;
    }

    /**
     * @return string
     * @Groups({"read", "write"})
     */
    public function getName() : string
    {
        return $this->name;
    }

    /**
     * @param UuidInterface $uuid
     */
    public function setUuid(UuidInterface $uuid) : void
    {
        $this->uuid = $uuid;
    }

    /**
     * @param string $name
     */
    public function setName(string $name) : void
    {
        $this->name = $name;
    }
}

如果您不想使用序列化组,请使用 @ApiResource 注释实体(即而不是 ApiResource())。

在api/config/packages/api_platform.yaml中通常有如下配置:

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']

如果 api 确实找到了您的资源,它可能类似于:

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/src/ApiPlatform']

但是为了找到您的实体及其映射,它应该像这样:

api_platform:
    mapping:
        paths: 
      - '%kernel.project_dir%/src/Entity'
      - '%kernel.project_dir%/src/Resources/config/doctrine'
      - '%kernel.project_dir%/src/ApiPlatform'

doctrine 查找映射的实际文件夹可能不同,请在 api/config/packages/doctrine.yaml 中查找 或配置学说的任何地方。