为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
Smart猫小萌 2022-06-11 10:34:43
当我想将学说实体与 API 平台注释分开时,我有一个案例。主要问题是我能够创建一个资源,看起来还可以,但它没有保存在数据库中。似乎我需要再添加一件事,但不知道在这里放什么。映射是正确的,数据库是最新的......实体类:<?phpdeclare(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 平台类<?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;/** * @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;    }}
查看完整描述

2 回答

?
茅侃侃

TA贡献1842条经验 获得超22个赞

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


api_platform:

    mapping:

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

如果 aip 确实找到了您的资源,它可能是这样的:


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'

教义查找映射的实际文件夹可能不同,请查看 api/config/packages/doctrine.yaml 或配置教义的任何位置。


查看完整回答
反对 回复 2022-06-11
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

首先,您的 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()) 注释实体。


查看完整回答
反对 回复 2022-06-11
  • 2 回答
  • 0 关注
  • 164 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号