如何扩展学说实体以使用 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;

    }

}



Smart猫小萌
浏览 165回答 2
2回答

茅侃侃

在 api/config/packages/api_platform.yaml 通常有以下配置:api_platform:&nbsp; &nbsp; mapping:&nbsp; &nbsp; &nbsp; &nbsp; paths: ['%kernel.project_dir%/src/Entity']如果 aip 确实找到了您的资源,它可能是这样的:api_platform:&nbsp; &nbsp; mapping:&nbsp; &nbsp; &nbsp; &nbsp; paths: ['%kernel.project_dir%/src/ApiPlatform']但是为了找到您的实体及其映射,它应该是这样的:api_platform:&nbsp; &nbsp; mapping:&nbsp; &nbsp; &nbsp; &nbsp; paths:&nbsp;&nbsp; &nbsp; &nbsp; - '%kernel.project_dir%/src/Entity'&nbsp; &nbsp; &nbsp; - '%kernel.project_dir%/src/Resources/config/doctrine'&nbsp; &nbsp; &nbsp; - '%kernel.project_dir%/src/ApiPlatform'教义查找映射的实际文件夹可能不同,请查看 api/config/packages/doctrine.yaml 或配置教义的任何位置。

慕尼黑5688855

首先,您的 Example 类缺少该$name属性。在 $uuid 之后添加private $name;,除非 EntityExample 具有该受保护/公共属性。您的 ApiResource() 注释类似乎对任何属性都没有序列化组注释。 https://api-platform.com/docs/core/serialization/<?phpdeclare(strict_types=1);namespace ApiPlatform;use ApiPlatform\Core\Annotation\ApiProperty;use ApiPlatform\Core\Annotation\ApiResource;use Entity\Example as EntityExample;use Ramsey\Uuid\UuidInterface;/**&nbsp; * @ApiResource(&nbsp; *&nbsp; &nbsp; &nbsp;normalizationContext={"groups"={"read"}},&nbsp; *&nbsp; &nbsp; &nbsp;denormalizationContext={"groups"={"write"}}&nbsp; * )&nbsp;*/class Example extends EntityExample{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @ApiProperty(identifier=true)&nbsp; &nbsp; &nbsp;* @var UuidInterface&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $uuid;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @Groups({"read", "write"})&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $name;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return UuidInterface&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getUuid() : UuidInterface&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->uuid;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return string&nbsp; &nbsp; &nbsp;* @Groups({"read", "write"})&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getName() : string&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->name;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param UuidInterface $uuid&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setUuid(UuidInterface $uuid) : void&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->uuid = $uuid;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param string $name&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setName(string $name) : void&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->name = $name;&nbsp; &nbsp; }}如果您不想使用序列化组,请使用@ApiResource(ie and not ApiResource()) 注释实体。
打开App,查看更多内容
随时随地看视频慕课网APP