gpt4 book ai didi

symfony - 使用 API Platform Symfony 继承实体

转载 作者:行者123 更新时间:2023-12-02 06:26:38 24 4
gpt4 key购买 nike

当我有一个从另一个实体继承的实体时,我遇到了平台 API ( https://api-platform.com/ ) 的问题。例如,继承自 User 实体的 Worker 实体。当我转到 Platform API 文档时,所有 Worker 属性都显示在 User 类中

<小时/>

与其说是解释,不如说是一个模式。这是我的两个实体以及存在问题的文档的结果

/**
* 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 方法时,它返回一个包含 Worker 实体属性的 User 实体

The result to see the problem

最佳答案

您的序列化需要更深入,以支持响应中序列化的工作器属性。我不确定如何具体使其适合您的实现。

JMS Serialization bundle ,看起来像这样:

/**
* Get the Birthday..
*
* @MaxDepth(2)
* @return \DateTime
*/
public function getBirthday(): \DateTime
{
return $this->birthday;
}

关于symfony - 使用 API Platform Symfony 继承实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45671292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com