我想用 API 平台注释分离教义实体时,我有一个情况。 主要问题是我能够创建一个资源,它似乎还可以,但它没有保存在数据库中。似乎我需要再添加一件事,但不知道在这里放什么。
映射正确,数据库是最新的...
实体类:
<?php
declare(strict_types=1);
namespace Entity;
use RamseyUuidUuidInterface;
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="EntityExample" 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 ApiPlatformCoreAnnotationApiProperty;
use ApiPlatformCoreAnnotationApiResource;
use EntityExample as EntityExample;
use RamseyUuidUuidInterface;
/**
* @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;
}
}
在 api/config/packages/api_platform.yaml 中,通常有以下配置:
api_platform:
mapping:
paths: ['%kernel.project_dir%/src/Entity']
如果apip确实找到了你的资源,它可能是这样的:
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中查找 或任何教义被配置的地方。
首先,您的 Example 类缺少$name
属性。 在$uuid后添加private $name;
,除非实体示例具有该受保护/公共属性。
您的 ApiResource(( 注释类似乎没有序列化组注释,对于任何属性。 https://api-platform.com/docs/core/serialization/
<?php
declare(strict_types=1);
namespace ApiPlatform;
use ApiPlatformCoreAnnotationApiProperty;
use ApiPlatformCoreAnnotationApiResource;
use EntityExample as EntityExample;
use RamseyUuidUuidInterface;
/**
* @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()
(对实体进行批注。