带API平台的继承实体Symfony
问题描述:
当我有一个从另一个实体继承的实体时,Platform API(https://api-platform.com/)存在问题。例如,从User实体继承的Worker实体。当我去到平台API文档的所有Worker属性出现在用户类带API平台的继承实体Symfony
胜于解释的模式。这里是我的两个实体和问题
/**
* User
*
* @ORM\Table(name="user")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"user" = "User", "worker" = "Worker"})
* @ApiResource
*
*/
class User
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=255)
*/
protected $lastname;
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", length=255)
*/
protected $firstname;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set lastname
*
* @param string $lastname
* @return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set firstname
*
* @param string $firstname
* @return User
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
}
/**
* Worker
*
* @ApiResource
*/
class Worker extends User
{
/**
* @var \DateTime
*
* @ORM\Column(name="birthday", type="datetime")
* @Assert\NotNull()
* @Assert\DateTime()
*/
private $birthday;
/**
* @var string
*
* @ORM\Column(name="mobilePhone", type="string", length=255, nullable=true)
*/
private $mobilePhone;
/**
* @return \DateTime
*/
public function getBirthday(): \DateTime
{
return $this->birthday;
}
/**
* @param \DateTime $birthday
*/
public function setBirthday(\DateTime $birthday)
{
$this->birthday = $birthday;
}
/**
* @return string
*/
public function getMobilePhone(): string
{
return ($this->mobilePhone == null) ? '' : $this->mobilePhone;
}
/**
* @param string $mobilePhone
*/
public function setMobilePhone(string $mobilePhone)
{
$this->mobilePhone = $mobilePhone;
}
}
文档的结果,这是问题。我们看到Worker类的属性出现在User类的模型中。而且,当我测试发送它返回一个包含工人实体的
谢谢所有
答
你的序列化需要去深度将增加一个级别的属性的用户实体POST方法支持响应中序列化的工作人员属性。我不确定如何专门使其适用于您的实施。
随着JMS Serialization bundle,看起来像这样:
/**
* Get the Birthday..
*
* @MaxDepth(2)
* @return \DateTime
*/
public function getBirthday(): \DateTime
{
return $this->birthday;
}
显示您的API文档注释请(此端点) –
我只是注释此PHP文件 – ascodix