使用API​​平台在多个不同的表中创建多个条目

时间:2018-12-27 16:47:06

标签: symfony api-platform.com

我正在为Symfony REST API使用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实体有关系,这是我必须向其表中添加数据的这两个实体。如何使用API​​平台执行此操作?

感谢您的帮助。

error

1 个答案:

答案 0 :(得分:0)

要将嵌入式实体保存在单个请求中,必须使用序列化程序组并启用级联持久化。

首先,将序列化程序组应用于您订单实体的非嵌入式属性。

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", ... }]
}

另一种替代方法是使用subresource,但是您必须先保存订单实体。