问题描述
class PropertyInput
{
...
/**
* @var LivingRoom[]
* @Groups({"property:write"})
*/
public $livingRooms;
...
}
/**
* @ApiResource (
* normalizationContext={"groups"={"livingroom:read"}},* attributes={"security"="is_granted(constant('\\App\\Model\\UserRoles::ADMIN'))"}
* )
* @ORM\Entity()
*/
class LivingRoom implements ResourceInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @Groups({"property:read","property:write","livingroom:read"})
*/
private int $id;
/**
* @ORM\Column(type="boolean",options={"default": 0})
* @Groups({"property:read","livingroom:read"})
*/
private bool $diningArea;
/**
* @ORM\Column(type="boolean","livingroom:read"})
*/
private bool $sofa;
/**
* @ORM\Column(type="boolean","livingroom:read"})
*/
private bool $TV;
/**
* @ORM\Column(type="boolean","livingroom:read"})
*/
private bool $coffeeTable;
/**
* @ORM\ManyToOne(targetEntity=Property::class,inversedBy="livingRoom")
* @ORM\JoinColumn(nullable=false,onDelete="CASCADE")
*/
private Property $property;
public function setId(int $id): void
{
$this->id = $id;
}
public function getId(): ?int
{
return $this->id;
}
public function getDiningArea(): ?bool
{
return $this->diningArea;
}
public function setDiningArea(bool $diningArea): self
{
$this->diningArea = $diningArea;
return $this;
}
public function getSofa(): ?bool
{
return $this->sofa;
}
public function setSofa(bool $sofa): self
{
$this->sofa = $sofa;
return $this;
}
public function getTV(): ?bool
{
return $this->TV;
}
public function setTV(bool $TV): self
{
$this->TV = $TV;
return $this;
}
public function getCoffeeTable(): ?bool
{
return $this->coffeeTable;
}
public function setCoffeeTable(bool $coffeeTable): self
{
$this->coffeeTable = $coffeeTable;
return $this;
}
public function getproperty(): ?Property
{
return $this->property;
}
public function setProperty(?Property $property): self
{
$this->property = $property;
return $this;
}
}
Property 和 LivingRoom 之间存在多对多关系。在创建和修补属性时,我们需要使用 DTO 方法。 当我们尝试使用 DTO 修补属性时,客厅 ID 不会从请求中解析或识别,因此每次都会创建新的客厅。
以下是修补属性请求的示例:
{"id":26,"propertyName":"Palace","bathrooms":0,"floors":0,"bedrooms":0,"livingRooms":[{"id":1,"TV":true,"coffeeTable": false,"diningArea":true,"sofa":true} ]}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)