问题描述
创建新产品时,我无法保存收藏集。
产品实体与ProductDetails具有一对多关系。
在ProductDetails中,我具有具有ColorList和SizeList实体的ManyToOne。
因此,ProductDetails包含ProductID,ColorID,SizeID。关系工作得很好,但是当我使用API Platfrom来保留productDetils时,它不起作用。
我也创建了一个EventSubscriber并拦截了POST_DESERIALIZE请求,而POST请求中针对ProductDetails的对象消失了。
我仍然得到201(创建对象)没有错误。
产品实体
/**
* @ApiResource(
* collectionoperations={"get","post"},* itemOperations={"get","put"},* normalizationContext={"groups"={"product:read"}},* denormalizationContext={"groups"={"product:write"}}
* )
* @ORM\Entity(repositoryClass=ProductRepository::class)
*/
class Product
{
use TimestampableEntity;
use SoftDeleteableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Store::class,inversedBy="products")
*/
private $store;
/**
* @ORM\Column(type="string",length=255)
* @Groups({"product:read","product:write"})
*/
private $name;
/**
* @ORM\Column(type="string","product:write"})
*/
private $barcode;
/**
* @ORM\OnetoMany(targetEntity=ProductDetails::class,mappedBy="product",cascade={"persist"})
* @Groups({"product:read","product:write"})
* @Assert\Valid()
*/
private $productDetails;
/**
* @ORM\Column(type="string","product:write"})
*/
private $sku;
/**
* @ORM\Column(type="string","product:write"})
*/
private $description;
/**
* @ORM\Column(type="string","product:write"})
*/
private $brand;
/**
* @ORM\Column(type="string","product:write"})
*/
private $productcondition;
产品详细信息实体
class ProductDetails
{
use TimestampableEntity;
use SoftDeleteableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Product::class,inversedBy="productDetails")
*/
private $product;
/**
* @ORM\Column(type="integer")
* @Groups({"product_detail:read","product_detail:write","product:read","product:write"})
*/
private $quantity;
/**
* @ApiSubresource()
* @ORM\ManyToOne(targetEntity=ColorList::class,inversedBy="productDetails")
* @Groups({"product_detail:read","product:write"})
*/
private $color;
/**
* @ApiSubresource()
* @ORM\ManyToOne(targetEntity=SizeList::class,"product:write"})
*/
private $size;
Getter & Setter (Update 1)
/**
* @return Collection|ProductDetails[]
*/
public function getProductDetails(): Collection
{
return $this->productDetails;
}
public function addProductDetails(ProductDetails $productDetails): self
{
if (!$this->productDetails->contains($productDetails)) {
$this->productDetails[] = $productDetails;
$productDetails->setProduct($this);
}
return $this;
}
public function removeProductDetails(ProductDetails $productDetails): self
{
if ($this->productDetails->contains($productDetails)) {
$this->productDetails->removeElement($productDetails);
// set the owning side to null (unless already changed)
if ($productDetails->getProduct() === $this) {
$productDetails->setProduct(null);
}
}
return $this;
}
我不确定为什么API没有映射集合,GET调用就可以了。
任何帮助都会很棒。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)