首页 > 解决方案 > 如何扩展学说实体以使用 API 平台

问题描述

当我想将学说实体与 API 平台注释分开时,我有一个案例。主要问题是我能够创建一个资源,看起来还可以,但它没有保存在数据库中。似乎我需要再添加一件事,但不知道在这里放什么。

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

实体类:

<?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 平台类

<?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;
    }
}

标签: phpsymfonydoctrine-ormapi-platform.com

解决方案


首先,您的 Example 类缺少该$name属性。在 $uuid 之后添加private $name;,除非 EntityExample 具有该受保护/公共属性。

您的 ApiResource() 注释类似乎对任何属性都没有序列化组注释。 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(ie and not ApiResource()) 注释实体。


推荐阅读