php - Api-platform - 虚拟属性 - 集合非规范化
问题描述
我有 Reservation 实体,其中包含一些 ReservationRev(isions) 集合。ReservationRev 包含 PersonStay 集合。
在预订中,我有虚拟财产 personStays(getter、add、remove、setter)。
当我尝试使用 personStays 发布预订时,我收到一个错误 - 找不到 Collection 的非规范化器。
班级预订
/**
* @var Collection
* @Doctrine\ORM\Mapping\OneToMany(
* targetEntity="Zakjakub\OswisAccommodationBundle\Entity\ReservationRev",
* mappedBy="container",
* cascade={"all"},
* orphanRemoval=true
* )
*/
protected $revisions;
public function __construct(Nameable $nameable, ...) {
$this->revisions = new ArrayCollection();
$this->addRevision(new ReservationRev($nameable));
...
}
final public function addPersonStay(?PersonStay $personStay): void
{
if (!$this->getPersonStays()->contains($personStay)) {
$newRevision = clone $this->getRevisionByDate();
$newRevision->addPersonStay($personStay);
$this->addRevision($newRevision);
}
}
final public function getPersonStays(?\DateTime $dateTime = null): Collection
{
return $this->getRevisionByDate($dateTime)->getPersonStays() ?? new ArrayCollection();
}
final public function removePersonStay(?PersonStay $personStay): void
{
if ($this->getPersonStays()->contains($personStay)) {
$newRevision = clone $this->getRevisionByDate();
$newRevision->removePersonStay($personStay);
$this->addRevision($newRevision);
}
}
final public function setPersonStays(?Collection $personStays): void
{
$newRevision = clone $this->getRevisionByDate();
$newRevision->setPersonStays($personStays ?? new ArrayCollection());
$this->addRevision($newRevision);
}
类 ReservationRev
/**
* @var Collection
* @Doctrine\ORM\Mapping\ManyToMany(
* targetEntity="Zakjakub\OswisAccommodationBundle\Entity\PersonStay",
* inversedBy="reservationRevisions",
* cascade={"all"}
* )
* @Doctrine\ORM\Mapping\JoinTable(name="accommodation_reservations_person_stays")
*/
protected $personStays;
/**
* @var Reservation
* @Doctrine\ORM\Mapping\ManyToOne(
* targetEntity="Zakjakub\OswisAccommodationBundle\Entity\Reservation",
* inversedBy="revisions"
* )
* @Doctrine\ORM\Mapping\JoinColumn(name="container_id", referencedColumnName="id")
*/
protected $container;
public function __construct(Nameable $nameable, ...) {
$this->personStays = new ArrayCollection();
...
}
final public function addPersonStay(?PersonStay $personStay): void
{
if ($personStay) {
return;
}
if (!$this->personStays->contains($personStay)) {
$this->personStays->add($personStay);
$personStay->addReservationRevision($this);
}
}
final public function removePersonStay(?PersonStay $personStay): void
{
if ($personStay) {
return;
}
if ($this->personStays->removeElement($personStay)) {
$personStay->removeReservationRevision($this);
}
}
final public function setPersonStays(Collection $personStays): void
{
foreach ($this->personStays as $personStay) {
if (!$personStays->contains($personStay)) {
$this->removePersonStay($personStay);
}
}
foreach ($personStays as $personStay) {
if (!$this->personStays->contains($personStay)) {
$this->addPersonStay($personStay);
}
}
}
/**
* @return Collection
*/
final public function getPersonStays(): Collection
{
return $this->personStays ?? new ArrayCollection();
}
类PersonStay
/**
* @var Person
* @Doctrine\ORM\Mapping\ManyToOne(
* targetEntity="Zakjakub\OswisAccommodationBundle\Entity\Person",
* inversedBy="personStays",
* cascade={"all"}
* )
* @Doctrine\ORM\Mapping\JoinColumn(name="person_id", referencedColumnName="id")
*/
protected $person;
/**
* @var Collection
* @Doctrine\ORM\Mapping\ManyToMany(
* targetEntity="Zakjakub\OswisAccommodationBundle\Entity\ReservationRev",
* mappedBy="personStays"
* )
*/
protected $reservationRevisions;
/**
* @var Room
* @Doctrine\ORM\Mapping\ManyToOne(
* targetEntity="Zakjakub\OswisAccommodationBundle\Entity\Room",
* inversedBy="personStays",
* cascade={"persist"}
* )
* @Doctrine\ORM\Mapping\JoinColumn(name="room_id", referencedColumnName="id")
*/
protected $room;
public function __construct(
?Person $person = null,
?ReservationRev $reservationRevision = null,
?Room $room = null,
...
) {
$this->setPerson($person);
$this->reservationRevisions = new ArrayCollection();
$this->addReservationRevision($reservationRevision);
$this->setRoom($room);
...
}
/**
* @param ReservationRev|null $reservationRev
*/
final public function addReservationRevision(?ReservationRev $reservationRev): void
{
if (!$reservationRev) {
return;
}
if (!$this->reservationRevisions->contains($reservationRev)) {
$this->reservationRevisions->add($reservationRev);
$reservationRev->addPersonStay($this);
}
}
/**
* @return Person
*/
final public function getPerson(): ?Person
{
return $this->person;
}
/**
* Set person.
*
* @param Person|null $person
*/
final public function setPerson(?Person $person): void
{
if (null !== $this->person) {
$this->person->removePersonStay($this);
}
if ($person && $this->person !== $person) {
$person->addPersonStay($this);
$this->person = $person;
}
}
/**
* @return Room|null
*/
final public function getRoom(): ?Room
{
return $this->room;
}
/**
* @param Room|null $room
*/
final public function setRoom(?Room $room): void
{
if (null !== $this->room) {
$this->room->removePersonStay($this);
}
if ($room && $this->room !== $room) {
$room->addPersonStay($this);
$this->room = $room;
}
}
/**
* @return Collection
*/
final public function getReservationRevisions(): Collection
{
return $this->reservationRevisions ?? new ArrayCollection();
}
/**
* @param ReservationRev|null $reservationRev
*/
final public function removeReservationRevision(?ReservationRev $reservationRev): void
{
if (!$reservationRev) {
return;
}
if ($this->reservationRevisions->removeElement($reservationRev)) {
$reservationRev->removePersonStay($this);
}
}
邮政
{
"personStays": null (or [] or [<some person stays>]),
"description": "string",
"note": "string"
}
错误
Could not denormalize object of type Doctrine\\Common\\Collections\\Collection, no supporting normalizer found.
Symfony 4.2.2
API平台2.3.6
解决方案
推荐阅读
- c++ - 我尝试创建一个小部件应用程序,但我得到错误:不是信号或插槽声明
- ruby-on-rails - Rails 5.2.2.1 ActiveRecord::RecordInvalid:验证失败
- mysql - 在 .NET Core 中执行 MySQL 存储过程时出现“MySqlException:表 'mysql.proc' 不存在”
- java - Google App Engine 无法启动 Dataflow 作业
- c++ - 使用 [] 访问字符串元素会打印随机结果
- r - 计算 r 中每个患者的持续时间
- javascript - 如何使用来自 Ajax JQuery 的响应更新 html 元素
- python - 使用pandas DataFrame 比较两个file_csv 找出差异并将F2 加入F1
- php - 函数内array_push(mainArr,subAssociativeArr)的PHP问题
- powerbi - 计算累计总计时如何提取月份