Symfony 父类/表中的API平台自定义标识符
我有使用继承映射理论继承的实体。我有一个用@ORM\PrePersist()生成的自定义标识符,它位于trait中,在父类中使用 我希望能够更新子类具有的属性,因此,我需要在子实体上运行端点 当我运行项操作时,api平台找不到资源Symfony 父类/表中的API平台自定义标识符,symfony,doctrine-orm,api-platform.com,Symfony,Doctrine Orm,Api Platform.com,我有使用继承映射理论继承的实体。我有一个用@ORM\PrePersist()生成的自定义标识符,它位于trait中,在父类中使用 我希望能够更新子类具有的属性,因此,我需要在子实体上运行端点 当我运行项操作时,api平台找不到资源 PATCH/api/childas/{hash} NotFoundHttpException 未找到 api平台,它不识别哈希作为标识符。将该id作为您的标识,即使它为false且哈希为true Trait生成用于标识资源的哈希 <?php namespace
PATCH/api/childas/{hash}
NotFoundHttpException
未找到
api平台,它不识别哈希作为标识符。将该id作为您的标识,即使它为false且哈希为true
Trait生成用于标识资源的哈希
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use Doctrine\ORM\Mapping as ORM;
trait HashableTrait
{
/**
* @ORM\Column(type="string", length=255)
* @ApiProperty(identifier=true)
*/
private $hash;
public function getHash(): ?string
{
return $this->hash;
}
/**
* @ORM\PrePersist()
*/
public function setHash()
{
$this->hash = \sha1(\random_bytes(10));
}
}
我曾考虑过使用数据提供程序,但我一直收到错误。错误是因为trait中的
hash
属性和父实体中的id
属性必须可以从要使用的实体访问
ORM使用反射类来获取有关属性及其注释的信息ReflectionClass::hasProperty
显然不允许查看父类中的私有属性
<?php
namespace App\Entity;
use App\Entity\HashableTrait;
/**
* @ORM\HasLifecycleCallbacks()
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="integer")
* @ORM\DiscriminatorMap({
* 1 = "App\Entity\ChildA",
* 2 = "App\Entity\ChildB"
* })
*/
class Parent
{
use HashableTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @ApiProperty(identifier=false)
*/
private $id;
public function getId(): ?int
{
return $this->id;
}
// Properties, setters, getters
}
<?php
namespace App\Entity;
class ChildA extends Parent
{
// Custom properties for ChildA
}
App\Entity\ChildA:
collectionOperations:
post: ~
itemOperations:
post: ~
get: ~
patch: ~
delete: ~