使用 API 平台在多个不同的表中创建多个条目
create multiple entries in multiple different tables with API Platform
我正在使用 API Symfony REST 平台 API。我用一条路线搜索,在几个不同的表中添加多个数据,因此是几个实体。这是我想去的路:
/api/order
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
*/
class Order
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $origin;
/**
* @ORM\Column(type="string", length=255)
*/
private $destination;
/**
* @ORM\Column(type="string", length=50)
*/
private $idTrackingMerchant;
/**
* @ORM\Column(type="string", length=50)
*/
private $idTrackingA;
/**
* @ORM\OneToMany(targetEntity="App\Entity\OrderHistory", mappedBy="orderId")
*/
private $orderHistories;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="orders")
* @ORM\JoinColumn(nullable=false)
*/
private $customer;
/**
* @ORM\Column(type="integer")
*/
private $weightMerchant;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $weightReal;
/**
* @ORM\Column(type="integer")
*/
private $taxeOctroi;
/**
* @ORM\Column(type="integer")
*/
private $taxeOctroiRegional;
/**
* @ORM\Column(type="integer")
*/
private $taxeTva;
/**
* @ORM\Column(type="integer")
*/
private $taxeOctroiMerchant;
/**
* @ORM\Column(type="integer")
*/
private $taxeOctroiRegionalMerchant;
/**
* @ORM\Column(type="integer")
*/
private $taxeTvaMerchant;
/**
* @ORM\Column(type="datetime")
*/
private $date_add;
/**
* @ORM\Column(type="datetime")
*/
private $date_upd;
/**
* @ORM\OneToMany(targetEntity="App\Entity\OrderDetail", mappedBy="orderId")
*/
private $orderDetails;
/**
* Order constructor.
*/
public function __construct()
{
$this->date_add = new \DateTime();
$this->date_upd = new \DateTime();
$this->orderHistories = new ArrayCollection();
$this->orderDetails = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return null|string
*/
public function getOrigin(): ?string
{
return $this->origin;
}
/**
* @param string $origin
* @return Order
*/
public function setOrigin(string $origin): self
{
$this->origin = $origin;
return $this;
}
/**
* @return null|string
*/
public function getDestination(): ?string
{
return $this->destination;
}
/**
* @param string $destination
* @return Order
*/
public function setDestination(string $destination): self
{
$this->destination = $destination;
return $this;
}
/**
* @return null|string
*/
public function getIdTrackingMerchant(): ?string
{
return $this->idTrackingMerchant;
}
/**
* @param string $idTrackingMerchant
* @return Order
*/
public function setIdTrackingMerchant(string $idTrackingMerchant): self
{
$this->idTrackingMerchant = $idTrackingMerchant;
return $this;
}
/**
* @return null|string
*/
public function getIdTrackingA(): ?string
{
return $this->idTrackingA;
}
/**
* @param string $idTrackingA
* @return Order
*/
public function setIdTrackingA(string $idTrackingA): self
{
$this->idTrackingA = $idTrackingA;
return $this;
}
/**
* @return Collection|OrderHistory[]
*/
public function getOrderHistories(): Collection
{
return $this->orderHistories;
}
/**
* @param OrderHistory $orderHistory
* @return Order
*/
public function addOrderHistory(OrderHistory $orderHistory): self
{
if (!$this->orderHistories->contains($orderHistory)) {
$this->orderHistories[] = $orderHistory;
$orderHistory->setOrderId($this);
}
return $this;
}
/**
* @param OrderHistory $orderHistory
* @return Order
*/
public function removeOrderHistory(OrderHistory $orderHistory): self
{
if ($this->orderHistories->contains($orderHistory)) {
$this->orderHistories->removeElement($orderHistory);
// set the owning side to null (unless already changed)
if ($orderHistory->getOrderId() === $this) {
$orderHistory->setOrderId(null);
}
}
return $this;
}
/**
* @return Customer|null
*/
public function getCustomer(): ?Customer
{
return $this->customer;
}
/**
* @param Customer|null $customer
* @return Order
*/
public function setCustomer(?Customer $customer): self
{
$this->customer = $customer;
return $this;
}
/**
* @return int|null
*/
public function getWeightMerchant(): ?int
{
return $this->weightMerchant;
}
/**
* @param int $weightMerchant
* @return Order
*/
public function setWeightMerchant(int $weightMerchant): self
{
$this->weightMerchant = $weightMerchant;
return $this;
}
/**
* @return int|null
*/
public function getWeightReal(): ?int
{
return $this->weightReal;
}
/**
* @param int|null $weightReal
* @return Order
*/
public function setWeightReal(?int $weightReal): self
{
$this->weightReal = $weightReal;
return $this;
}
/**
* @return int|null
*/
public function getTaxeTva(): ?int
{
return $this->taxeTva;
}
/**
* @param int $taxeTva
* @return Order
*/
public function setTaxeTva(int $taxeTva): self
{
$this->taxeTva = $taxeTva;
return $this;
}
/**
* @return int|null
*/
public function getTaxeOctroi(): ?int
{
return $this->taxeOctroi;
}
/**
* @param int $taxeOctroi
* @return Order
*/
public function setTaxeOctroi(int $taxeOctroi): self
{
$this->taxeOctroi = $taxeOctroi;
return $this;
}
/**
* @return int|null
*/
public function getTaxeOctroiRegional(): ?int
{
return $this->taxeOctroiRegional;
}
/**
* @param int $taxeOctroiRegional
* @return Order
*/
public function setTaxeOctroiRegional(int $taxeOctroiRegional): self
{
$this->taxeOctroiRegional = $taxeOctroiRegional;
return $this;
}
/**
* @return int|null
*/
public function getTaxeTvaMerchant(): ?int
{
return $this->taxeTvaMerchant;
}
/**
* @param int $taxeTvaMerchant
* @return Order
*/
public function setTaxeTvaMerchant(int $taxeTvaMerchant): self
{
$this->taxeTvaMerchant = $taxeTvaMerchant;
return $this;
}
/**
* @return int|null
*/
public function getTaxeOctroiMerchant(): ?int
{
return $this->taxeOctroiMerchant;
}
/**
* @param int $taxeOctroiMerchant
* @return Order
*/
public function setTaxeOctroiMerchant(int $taxeOctroiMerchant): self
{
$this->taxeOctroiMerchant = $taxeOctroiMerchant;
return $this;
}
/**
* @return int|null
*/
public function getTaxeOctroiRegionalMerchant(): ?int
{
return $this->taxeOctroiRegionalMerchant;
}
/**
* @param int $taxeOctroiRegionalMerchant
* @return Order
*/
public function setTaxeOctroiRegionalMerchant(int $taxeOctroiRegionalMerchant): self
{
$this->taxeOctroiRegionalMerchant = $taxeOctroiRegionalMerchant;
return $this;
}
/**
* @return \DateTimeInterface|null
*/
public function getDateAdd(): ?\DateTimeInterface
{
return $this->date_add;
}
/**
* @return \DateTimeInterface|null
*/
public function getDateUpd(): ?\DateTimeInterface
{
return $this->date_upd;
}
/**
* @param \DateTimeInterface $date_upd
* @return Order
*/
public function setDateUpd(\DateTimeInterface $date_upd): self
{
$this->date_upd = $date_upd;
return $this;
}
/**
* @return Collection|OrderDetail[]
*/
public function getOrderDetails(): Collection
{
return $this->orderDetails;
}
/**
* @param OrderDetail $orderDetail
* @return Order
*/
public function addOrderDetail(OrderDetail $orderDetail): self
{
if (!$this->orderDetails->contains($orderDetail)) {
$this->orderDetails[] = $orderDetail;
$orderDetail->setOrderId($this);
}
return $this;
}
/**
* @param OrderDetail $orderDetail
* @return Order
*/
public function removeOrderDetail(OrderDetail $orderDetail): self
{
if ($this->orderDetails->contains($orderDetail)) {
$this->orderDetails->removeElement($orderDetail);
// set the owning side to null (unless already changed)
if ($orderDetail->getOrderId() === $this) {
$orderDetail->setOrderId(null);
}
}
return $this;
}
}
此实体与实体 OrderDetail 和 OrderHistory 有关系,我必须将数据添加到它们的表中的是这 2 个实体。如何使用 API 平台执行此操作?
感谢您的帮助。
要在单个请求中保存嵌入式实体,您必须使用序列化程序组并启用级联持久化。
首先,将序列化程序组应用于您的订单实体非嵌入式属性。
use Symfony\Component\Serializer\Annotation\Groups;
...
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups("order")
*/
private $id;
接下来将组应用到您的嵌入式属性并添加级联选项:
/**
* @ORM\OneToMany(targetEntity="App\Entity\OrderDetail", mappedBy="orderId", cascade={"persist", "remove"})
* @Groups("order:order_detail")
*/
private $orderDetails;
接下来,将组应用于您的嵌入式实体属性:
public class OrderDetail {
/**
* ...
* @Groups("order_detail")
*/
private $id;
...
}
并将它们添加到序列化上下文中,如果您不想在检索数据时嵌入它们,您可以从 normalizationContext
:
中删除组
/**
* @ApiResource(
* normalizationContext={"groups"={"order", "order:order_detail", "order_detail"}},
* denormalizationContext={"groups"={"order", "order:order_detail", "order_detail"}}
*)
* @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
*/
class Order { }
现在,您可以使用单个请求保存它:
POST /orders
{
...
orderDetails: [{ "myProperty": "myValue", ... }]
}
我正在使用 API Symfony REST 平台 API。我用一条路线搜索,在几个不同的表中添加多个数据,因此是几个实体。这是我想去的路:
/api/order
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
*/
class Order
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $origin;
/**
* @ORM\Column(type="string", length=255)
*/
private $destination;
/**
* @ORM\Column(type="string", length=50)
*/
private $idTrackingMerchant;
/**
* @ORM\Column(type="string", length=50)
*/
private $idTrackingA;
/**
* @ORM\OneToMany(targetEntity="App\Entity\OrderHistory", mappedBy="orderId")
*/
private $orderHistories;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="orders")
* @ORM\JoinColumn(nullable=false)
*/
private $customer;
/**
* @ORM\Column(type="integer")
*/
private $weightMerchant;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $weightReal;
/**
* @ORM\Column(type="integer")
*/
private $taxeOctroi;
/**
* @ORM\Column(type="integer")
*/
private $taxeOctroiRegional;
/**
* @ORM\Column(type="integer")
*/
private $taxeTva;
/**
* @ORM\Column(type="integer")
*/
private $taxeOctroiMerchant;
/**
* @ORM\Column(type="integer")
*/
private $taxeOctroiRegionalMerchant;
/**
* @ORM\Column(type="integer")
*/
private $taxeTvaMerchant;
/**
* @ORM\Column(type="datetime")
*/
private $date_add;
/**
* @ORM\Column(type="datetime")
*/
private $date_upd;
/**
* @ORM\OneToMany(targetEntity="App\Entity\OrderDetail", mappedBy="orderId")
*/
private $orderDetails;
/**
* Order constructor.
*/
public function __construct()
{
$this->date_add = new \DateTime();
$this->date_upd = new \DateTime();
$this->orderHistories = new ArrayCollection();
$this->orderDetails = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return null|string
*/
public function getOrigin(): ?string
{
return $this->origin;
}
/**
* @param string $origin
* @return Order
*/
public function setOrigin(string $origin): self
{
$this->origin = $origin;
return $this;
}
/**
* @return null|string
*/
public function getDestination(): ?string
{
return $this->destination;
}
/**
* @param string $destination
* @return Order
*/
public function setDestination(string $destination): self
{
$this->destination = $destination;
return $this;
}
/**
* @return null|string
*/
public function getIdTrackingMerchant(): ?string
{
return $this->idTrackingMerchant;
}
/**
* @param string $idTrackingMerchant
* @return Order
*/
public function setIdTrackingMerchant(string $idTrackingMerchant): self
{
$this->idTrackingMerchant = $idTrackingMerchant;
return $this;
}
/**
* @return null|string
*/
public function getIdTrackingA(): ?string
{
return $this->idTrackingA;
}
/**
* @param string $idTrackingA
* @return Order
*/
public function setIdTrackingA(string $idTrackingA): self
{
$this->idTrackingA = $idTrackingA;
return $this;
}
/**
* @return Collection|OrderHistory[]
*/
public function getOrderHistories(): Collection
{
return $this->orderHistories;
}
/**
* @param OrderHistory $orderHistory
* @return Order
*/
public function addOrderHistory(OrderHistory $orderHistory): self
{
if (!$this->orderHistories->contains($orderHistory)) {
$this->orderHistories[] = $orderHistory;
$orderHistory->setOrderId($this);
}
return $this;
}
/**
* @param OrderHistory $orderHistory
* @return Order
*/
public function removeOrderHistory(OrderHistory $orderHistory): self
{
if ($this->orderHistories->contains($orderHistory)) {
$this->orderHistories->removeElement($orderHistory);
// set the owning side to null (unless already changed)
if ($orderHistory->getOrderId() === $this) {
$orderHistory->setOrderId(null);
}
}
return $this;
}
/**
* @return Customer|null
*/
public function getCustomer(): ?Customer
{
return $this->customer;
}
/**
* @param Customer|null $customer
* @return Order
*/
public function setCustomer(?Customer $customer): self
{
$this->customer = $customer;
return $this;
}
/**
* @return int|null
*/
public function getWeightMerchant(): ?int
{
return $this->weightMerchant;
}
/**
* @param int $weightMerchant
* @return Order
*/
public function setWeightMerchant(int $weightMerchant): self
{
$this->weightMerchant = $weightMerchant;
return $this;
}
/**
* @return int|null
*/
public function getWeightReal(): ?int
{
return $this->weightReal;
}
/**
* @param int|null $weightReal
* @return Order
*/
public function setWeightReal(?int $weightReal): self
{
$this->weightReal = $weightReal;
return $this;
}
/**
* @return int|null
*/
public function getTaxeTva(): ?int
{
return $this->taxeTva;
}
/**
* @param int $taxeTva
* @return Order
*/
public function setTaxeTva(int $taxeTva): self
{
$this->taxeTva = $taxeTva;
return $this;
}
/**
* @return int|null
*/
public function getTaxeOctroi(): ?int
{
return $this->taxeOctroi;
}
/**
* @param int $taxeOctroi
* @return Order
*/
public function setTaxeOctroi(int $taxeOctroi): self
{
$this->taxeOctroi = $taxeOctroi;
return $this;
}
/**
* @return int|null
*/
public function getTaxeOctroiRegional(): ?int
{
return $this->taxeOctroiRegional;
}
/**
* @param int $taxeOctroiRegional
* @return Order
*/
public function setTaxeOctroiRegional(int $taxeOctroiRegional): self
{
$this->taxeOctroiRegional = $taxeOctroiRegional;
return $this;
}
/**
* @return int|null
*/
public function getTaxeTvaMerchant(): ?int
{
return $this->taxeTvaMerchant;
}
/**
* @param int $taxeTvaMerchant
* @return Order
*/
public function setTaxeTvaMerchant(int $taxeTvaMerchant): self
{
$this->taxeTvaMerchant = $taxeTvaMerchant;
return $this;
}
/**
* @return int|null
*/
public function getTaxeOctroiMerchant(): ?int
{
return $this->taxeOctroiMerchant;
}
/**
* @param int $taxeOctroiMerchant
* @return Order
*/
public function setTaxeOctroiMerchant(int $taxeOctroiMerchant): self
{
$this->taxeOctroiMerchant = $taxeOctroiMerchant;
return $this;
}
/**
* @return int|null
*/
public function getTaxeOctroiRegionalMerchant(): ?int
{
return $this->taxeOctroiRegionalMerchant;
}
/**
* @param int $taxeOctroiRegionalMerchant
* @return Order
*/
public function setTaxeOctroiRegionalMerchant(int $taxeOctroiRegionalMerchant): self
{
$this->taxeOctroiRegionalMerchant = $taxeOctroiRegionalMerchant;
return $this;
}
/**
* @return \DateTimeInterface|null
*/
public function getDateAdd(): ?\DateTimeInterface
{
return $this->date_add;
}
/**
* @return \DateTimeInterface|null
*/
public function getDateUpd(): ?\DateTimeInterface
{
return $this->date_upd;
}
/**
* @param \DateTimeInterface $date_upd
* @return Order
*/
public function setDateUpd(\DateTimeInterface $date_upd): self
{
$this->date_upd = $date_upd;
return $this;
}
/**
* @return Collection|OrderDetail[]
*/
public function getOrderDetails(): Collection
{
return $this->orderDetails;
}
/**
* @param OrderDetail $orderDetail
* @return Order
*/
public function addOrderDetail(OrderDetail $orderDetail): self
{
if (!$this->orderDetails->contains($orderDetail)) {
$this->orderDetails[] = $orderDetail;
$orderDetail->setOrderId($this);
}
return $this;
}
/**
* @param OrderDetail $orderDetail
* @return Order
*/
public function removeOrderDetail(OrderDetail $orderDetail): self
{
if ($this->orderDetails->contains($orderDetail)) {
$this->orderDetails->removeElement($orderDetail);
// set the owning side to null (unless already changed)
if ($orderDetail->getOrderId() === $this) {
$orderDetail->setOrderId(null);
}
}
return $this;
}
}
此实体与实体 OrderDetail 和 OrderHistory 有关系,我必须将数据添加到它们的表中的是这 2 个实体。如何使用 API 平台执行此操作?
感谢您的帮助。
要在单个请求中保存嵌入式实体,您必须使用序列化程序组并启用级联持久化。
首先,将序列化程序组应用于您的订单实体非嵌入式属性。
use Symfony\Component\Serializer\Annotation\Groups;
...
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups("order")
*/
private $id;
接下来将组应用到您的嵌入式属性并添加级联选项:
/**
* @ORM\OneToMany(targetEntity="App\Entity\OrderDetail", mappedBy="orderId", cascade={"persist", "remove"})
* @Groups("order:order_detail")
*/
private $orderDetails;
接下来,将组应用于您的嵌入式实体属性:
public class OrderDetail {
/**
* ...
* @Groups("order_detail")
*/
private $id;
...
}
并将它们添加到序列化上下文中,如果您不想在检索数据时嵌入它们,您可以从 normalizationContext
:
/**
* @ApiResource(
* normalizationContext={"groups"={"order", "order:order_detail", "order_detail"}},
* denormalizationContext={"groups"={"order", "order:order_detail", "order_detail"}}
*)
* @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
*/
class Order { }
现在,您可以使用单个请求保存它:
POST /orders
{
...
orderDetails: [{ "myProperty": "myValue", ... }]
}