php - 如何扩展学说实体以使用 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;
}
}
解决方案
首先,您的 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()
) 注释实体。
推荐阅读
- javascript - 每 10 秒使用 querySelector 更改背景图像不起作用
- python - tf.scatter_update 或 tf.scatter_nd_update 可以用于更新张量的列切片吗?
- javascript - 无法使用 require.js 加载 jquery
- jenkins - 为什么詹金斯反复查询旧的声纳 qube 分析?
- c++ - 无法读取所有行的数据
- reactjs - 使用 MockedProvider 进行测试时使用 Apollo 突变更新存储
- sql - 如何在查询结果中添加结尾零
- jquery - 在多个基于查询日期选择器的日历中选择日期和可用性
- android - 由于带有 https url 的设备日期时间错误,无法在 Android 中加载 api 响应
- linux - 汇编程序在 GDB 中找不到调试符号