首页 > 解决方案 > 多对一关系 symfony4

问题描述

我在 Symfony4 下使用 Api Platform 实现了一个 API,但在 ManyToOne 中检索关系信息时遇到问题。

我有一个与 Customer 表相关的 Order 实体,当我创建 $ data-> getCustomer () 时,我只得到客户 ID 而不是其他数据。

我的订单实体

<?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;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;

/**
 * @ApiResource(
 *     collectionOperations={
 *         "get"={
 *             "method"="GET",
 *             "normalization_context"={"groups"={"orderGET", "orderGetCollection"}},
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         },
 *         "post"={
 *             "method"="POST",
 *             "denormalization_context"={"groups"={"orderPostCustomer"}},
 *             "access_control"="is_granted('ROLE_CUSTOMER')"
 *         }
 *     },
 *     itemOperations={
 *         "getItem"={
 *             "method"="GET",
 *             "normalization_context"={"groups"={"orderGET", "orderGetItem"}},
 *             "access_control"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CUSTOMER')"
 *         },
 *         "put"={
 *             "method"="PUT",
 *             "normalization_context"={"groups"={"orderPostCustomer"}},
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         }
 *     }
 *  )
 * @ApiFilter(SearchFilter::class, properties={"idTrackingAkaz": "exact", "lastHistory.status": "exact", "customer.city": "partial", "city": "partial", "lastHistory.User": "exact"})
 * @ApiFilter(DateFilter::class, properties={"dateAdd"})
 * @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
 */
class Order
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"orderGET", "orderDELETE", "orderPostCustomer"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $phone;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $address;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $address1;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $zipcode;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGET", "orderPostCustomer"})
     */
    private $city;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $country;

    /**
     * @ORM\Column(type="string", length=2000, nullable=true)
     * @Groups("orderGetItem")
     */
    private $urlTracking;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $idOrderMerchant;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @Groups({"orderGetItem"})
     */
    private $idTrackingMerchant;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @Groups("orderGET")
     */
    private $idTrackingAkaz;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\OrderHistory", mappedBy="orderId", cascade={"persist", "remove"})
     * @Groups("orderGetItem")
     */
    private $orderHistories;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\OrderHistory", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="last_history_id", referencedColumnName="id")
     * @Groups("orderGetCollection")
     */
    private $lastHistory;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="orders")
     * @ORM\JoinColumn(nullable=true)
     * @Groups("orderGET")
     */
    private $customer;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $weightMerchant;

    /**
     * @ORM\Column(type="integer", nullable=true)
     * @Groups("orderGetItem")
     */
    private $weightReal;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $packages;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("orderGET")
     */
    private $dateAdd;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("orderGET")
     */
    private $dateUpd;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\OrderDetail", mappedBy="orderId", cascade={"persist", "remove"})
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $orderDetails;

    /**
     * Order constructor.
     */
    public function __construct()
    {
        $this->dateAdd = new \DateTime();
        $this->dateUpd = 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 getPhone(): ?string
    {
        return $this->phone;
    }

    /**
     * @param string $phone
     * @return Order
     */
    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getAddress(): ?string
    {
        return $this->address;
    }

    /**
     * @param string $address
     * @return Order
     */
    public function setAddress(string $address): self
    {
        $this->address = $address;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getAddress1(): ?string
    {
        return $this->address1;
    }

    /**
     * @param string $address1
     * @return Order
     */
    public function setAddress1(string $address1): self
    {
        $this->address1 = $address1;

        return $this;
    }

    /**
     * @return int|null
     */
    public function getZipcode(): ?int
    {
        return $this->zipcode;
    }

    /**
     * @param int $zipcode
     * @return Order
     */
    public function setZipcode(int $zipcode): self
    {
        $this->zipcode = $zipcode;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getCity(): ?string
    {
        return $this->city;
    }

    /**
     * @param string $city
     * @return Order
     */
    public function setCity(string $city): self
    {
        $this->city = $city;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getCountry(): ?string
    {
        return $this->country;
    }

    /**
     * @param string $country
     * @return Order
     */
    public function setCountry(string $country): self
    {
        $this->country = $country;

        return $this;
    }

    /**
     * @return null|int
     */
    public function getIdOrderMerchant(): ?int
    {
        return $this->idOrderMerchant;
    }

    /**
     * @param int $idOrderMerchant
     * @return Order
     */
    public function setIdOrderMerchant(int $idOrderMerchant): self
    {
        $this->idOrderMerchant = $idOrderMerchant;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getUrlTracking(): ?string
    {
        return $this->urlTracking;
    }

    /**
     * @param string $urlTracking
     * @return Order
     */
    public function setUrlTracking(string $urlTracking): self
    {
        $this->urlTracking = $urlTracking;

        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 getIdTrackingAkaz(): ?string
    {
        return $this->idTrackingAkaz;
    }

    /**
     * @param string $idTrackingAkaz
     * @return Order
     */
    public function setIdTrackingAkaz(string $idTrackingAkaz): self
    {
        $this->idTrackingAkaz = $idTrackingAkaz;

        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 mixed
     */
    public function getLastHistory()
    {
        return $this->lastHistory;
    }

    /**
     * @param mixed $lastHistory
     */
    public function setLastHistory(OrderHistory $lastHistory): void
    {
        $this->lastHistory = $lastHistory;
    }

    /**
     * @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;
    }

    public function getDateAdd()
    {
        return $this->dateAdd;
    }

    /**
     * @return \DateTimeInterface|null
     */
    public function getDateUpd(): ?\DateTimeInterface
    {
        return $this->dateUpd;
    }

    /**
     * @param \DateTimeInterface $dateUpd
     * @return Order
     */
    public function setDateUpd(\DateTimeInterface $dateUpd): self
    {
        $this->dateUpd = $dateUpd;

        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;
    }

    /**
     * @return mixed
     */
    public function getPackages()
    {
        return $this->packages;
    }

    /**
     * @param mixed $packages
     */
    public function setPackages($packages): void
    {
        $this->packages = $packages;
    }
}

我的客户实体

<?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;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;

/**
 * @ApiResource(
 *     attributes={"access_control"="is_granted('ROLE_ADMIN')"},
 *     normalizationContext={"groups"={"customer", "customer:user", "user"}},
 *     denormalizationContext={"groups"={"customer", "customer:user", "user"}}
 *  )
 * @ApiFilter(SearchFilter::class, properties={"id": "exact", "company": "exact", "website": "exact", "user.isActive": "exact"})
 * @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
 */
class Customer
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"orderGET", "customer"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=50)
     * @Groups({"orderGetItem", "customer"})
     */
    private $company;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $website;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $siret;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $ape;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $phone;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $address;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups("customer")
     */
    private $address1;

    /**
     * @ORM\Column(type="integer")
     * @Groups("customer")
     */
    private $zipcode;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGET", "customer"})
     */
    private $city;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $country;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @Groups("customer")
     */
    private $note;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("customer")
     */
    private $dateAdd;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("customer")
     */
    private $dateUpd;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     * @Groups({"customer:user", "orderGetItem"})
     */
    private $user;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Order", mappedBy="customer")
     * @Groups("customer")
     */
    private $orders;

    /**
     * Customer constructor.
     */
    public function __construct()
    {
        $this->dateAdd = new \DateTime();
        $this->dateUpd = new \DateTime();
        $this->orders = new ArrayCollection();
    }

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return null|string
     */
    public function getCompany(): ?string
    {
        return $this->company;
    }

    /**
     * @param string $company
     * @return Customer
     */
    public function setCompany(string $company): self
    {
        $this->company = $company;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getWebsite(): ?string
    {
        return $this->website;
    }

    /**
     * @param string $website
     * @return Customer
     */
    public function setWebsite(string $website): self
    {
        $this->website = $website;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getSiret(): ?string
    {
        return $this->siret;
    }

    /**
     * @param string $siret
     * @return Customer
     */
    public function setSiret(string $siret): self
    {
        $this->siret = $siret;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getApe(): ?string
    {
        return $this->ape;
    }

    /**
     * @param string $ape
     * @return Customer
     */
    public function setApe(string $ape): self
    {
        $this->ape = $ape;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getPhone(): ?string
    {
        return $this->phone;
    }

    /**
     * @param string $phone
     * @return Customer
     */
    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getAddress(): ?string
    {
        return $this->address;
    }

    /**
     * @param string $address
     * @return Customer
     */
    public function setAddress(string $address): self
    {
        $this->address = $address;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getAddress1(): ?string
    {
        return $this->address1;
    }

    /**
     * @param string $address1
     * @return Customer
     */
    public function setAddress1(string $address1): self
    {
        $this->address1 = $address1;

        return $this;
    }

    /**
     * @return int|null
     */
    public function getZipcode(): ?int
    {
        return $this->zipcode;
    }

    /**
     * @param int $zipcode
     * @return Customer
     */
    public function setZipcode(int $zipcode): self
    {
        $this->zipcode = $zipcode;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getCity(): ?string
    {
        return $this->city;
    }

    /**
     * @param string $city
     * @return Customer
     */
    public function setCity(string $city): self
    {
        $this->city = $city;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getCountry(): ?string
    {
        return $this->country;
    }

    /**
     * @param string $country
     * @return Customer
     */
    public function setCountry(string $country): self
    {
        $this->country = $country;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getNote(): ?string
    {
        return $this->note;
    }

    /**
     * @param string $note
     * @return Customer
     */
    public function setNote(string $note): self
    {
        $this->note = $note;

        return $this;
    }

    /**
     * @return \DateTimeInterface|null
     */
    public function getDateAdd(): ?\DateTimeInterface
    {
        return $this->dateAdd;
    }

    /**
     * @return \DateTimeInterface|null
     */
    public function getDateUpd(): ?\DateTimeInterface
    {
        return $this->dateUpd;
    }

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user): void
    {
        $this->user = $user;
    }

    /**
     * @return Collection|Order[]
     */
    public function getOrders(): Collection
    {
        return $this->orders;
    }

    /**
     * @param Order $order
     * @return Customer
     */
    public function addOrder(Order $order): self
    {
        if (!$this->orders->contains($order)) {
            $this->orders[] = $order;
            $order->setCustomer($this);
        }

        return $this;
    }

    /**
     * @param Order $order
     * @return Customer
     */
    public function removeOrder(Order $order): self
    {
        if ($this->orders->contains($order)) {
            $this->orders->removeElement($order);
            // set the owning side to null (unless already changed)
            if ($order->getCustomer() === $this) {
                $order->setCustomer(null);
            }
        }

        return $this;
    }
}

我真的不知道问题出在哪里。谢谢你的帮助。

标签: symfony4api-platform.com

解决方案


您使用OrderGet组在您的订单中获取您的客户,但您没有在您需要的所有数据中添加orderGet,您只需在您的 Id 中添加它。我认为您必须将此组添加到您需要的数据中。并且不要忘记将 orderGet添加到您的normalizationContext中,如下所示:

客户.php

<?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;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;

/**
 * @ApiResource(
 *     attributes={"access_control"="is_granted('ROLE_ADMIN')"},
 *     normalizationContext={"groups"={"customer", "customer:user", "user", "orderGET"}},
 *     denormalizationContext={"groups"={"customer", "customer:user", "user"}}
 *  )
 * @ApiFilter(SearchFilter::class, properties={"id": "exact", "company": "exact", "website": "exact", "user.isActive": "exact"})
 * @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
 */
class Customer
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"orderGET", "customer"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=50)
     * @Groups({"orderGetItem", "customer"})
     */
    private $company;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $website;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $siret;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $ape;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $phone;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $address;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups("customer")
     */
    private $address1;

    /**
     * @ORM\Column(type="integer")
     * @Groups("customer")
     */
    private $zipcode;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGET", "customer"})
     */
    private $city;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("customer")
     */
    private $country;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @Groups("customer")
     */
    private $note;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("customer")
     */
    private $dateAdd;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("customer")
     */
    private $dateUpd;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     * @Groups({"customer:user", "orderGetItem"})
     */
    private $user;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Order", mappedBy="customer")
     * @Groups("customer")
     */
    private $orders;

    /**
     * Customer constructor.
     */
    public function __construct()
    {
        $this->dateAdd = new \DateTime();
        $this->dateUpd = new \DateTime();
        $this->orders = new ArrayCollection();
    }

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return null|string
     */
    public function getCompany(): ?string
    {
        return $this->company;
    }

    /**
     * @param string $company
     * @return Customer
     */
    public function setCompany(string $company): self
    {
        $this->company = $company;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getWebsite(): ?string
    {
        return $this->website;
    }

    /**
     * @param string $website
     * @return Customer
     */
    public function setWebsite(string $website): self
    {
        $this->website = $website;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getSiret(): ?string
    {
        return $this->siret;
    }

    /**
     * @param string $siret
     * @return Customer
     */
    public function setSiret(string $siret): self
    {
        $this->siret = $siret;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getApe(): ?string
    {
        return $this->ape;
    }

    /**
     * @param string $ape
     * @return Customer
     */
    public function setApe(string $ape): self
    {
        $this->ape = $ape;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getPhone(): ?string
    {
        return $this->phone;
    }

    /**
     * @param string $phone
     * @return Customer
     */
    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getAddress(): ?string
    {
        return $this->address;
    }

    /**
     * @param string $address
     * @return Customer
     */
    public function setAddress(string $address): self
    {
        $this->address = $address;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getAddress1(): ?string
    {
        return $this->address1;
    }

    /**
     * @param string $address1
     * @return Customer
     */
    public function setAddress1(string $address1): self
    {
        $this->address1 = $address1;

        return $this;
    }

    /**
     * @return int|null
     */
    public function getZipcode(): ?int
    {
        return $this->zipcode;
    }

    /**
     * @param int $zipcode
     * @return Customer
     */
    public function setZipcode(int $zipcode): self
    {
        $this->zipcode = $zipcode;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getCity(): ?string
    {
        return $this->city;
    }

    /**
     * @param string $city
     * @return Customer
     */
    public function setCity(string $city): self
    {
        $this->city = $city;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getCountry(): ?string
    {
        return $this->country;
    }

    /**
     * @param string $country
     * @return Customer
     */
    public function setCountry(string $country): self
    {
        $this->country = $country;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getNote(): ?string
    {
        return $this->note;
    }

    /**
     * @param string $note
     * @return Customer
     */
    public function setNote(string $note): self
    {
        $this->note = $note;

        return $this;
    }

    /**
     * @return \DateTimeInterface|null
     */
    public function getDateAdd(): ?\DateTimeInterface
    {
        return $this->dateAdd;
    }

    /**
     * @return \DateTimeInterface|null
     */
    public function getDateUpd(): ?\DateTimeInterface
    {
        return $this->dateUpd;
    }

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user): void
    {
        $this->user = $user;
    }

    /**
     * @return Collection|Order[]
     */
    public function getOrders(): Collection
    {
        return $this->orders;
    }

    /**
     * @param Order $order
     * @return Customer
     */
    public function addOrder(Order $order): self
    {
        if (!$this->orders->contains($order)) {
            $this->orders[] = $order;
            $order->setCustomer($this);
        }

        return $this;
    }

    /**
     * @param Order $order
     * @return Customer
     */
    public function removeOrder(Order $order): self
    {
        if ($this->orders->contains($order)) {
            $this->orders->removeElement($order);
            // set the owning side to null (unless already changed)
            if ($order->getCustomer() === $this) {
                $order->setCustomer(null);
            }
        }

        return $this;
    }
}

订单.php

<?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;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"orderGET"}},
 *     collectionOperations={
 *         "get"={
 *             "method"="GET",
 *             "normalization_context"={"groups"={"orderGET", "orderGetCollection"}},
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         },
 *         "post"={
 *             "method"="POST",
 *             "denormalization_context"={"groups"={"orderPostCustomer"}},
 *             "access_control"="is_granted('ROLE_CUSTOMER')"
 *         }
 *     },
 *     itemOperations={
 *         "getItem"={
 *             "method"="GET",
 *             "normalization_context"={"groups"={"orderGET", "orderGetItem"}},
 *             "access_control"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CUSTOMER')"
 *         },
 *         "put"={
 *             "method"="PUT",
 *             "normalization_context"={"groups"={"orderPostCustomer"}},
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         }
 *     }
 *  )
 * @ApiFilter(SearchFilter::class, properties={"idTrackingAkaz": "exact", "lastHistory.status": "exact", "customer.city": "partial", "city": "partial", "lastHistory.User": "exact"})
 * @ApiFilter(DateFilter::class, properties={"dateAdd"})
 * @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
 */
class Order
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"orderGET", "orderDELETE", "orderPostCustomer"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $phone;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $address;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $address1;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $zipcode;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGET", "orderPostCustomer"})
     */
    private $city;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $country;

    /**
     * @ORM\Column(type="string", length=2000, nullable=true)
     * @Groups("orderGetItem")
     */
    private $urlTracking;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $idOrderMerchant;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @Groups({"orderGetItem"})
     */
    private $idTrackingMerchant;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @Groups("orderGET")
     */
    private $idTrackingAkaz;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\OrderHistory", mappedBy="orderId", cascade={"persist", "remove"})
     * @Groups("orderGetItem")
     */
    private $orderHistories;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\OrderHistory", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="last_history_id", referencedColumnName="id")
     * @Groups("orderGetCollection")
     */
    private $lastHistory;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="orders")
     * @ORM\JoinColumn(nullable=true)
     * @Groups("orderGET")
     */
    private $customer;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $weightMerchant;

    /**
     * @ORM\Column(type="integer", nullable=true)
     * @Groups("orderGetItem")
     */
    private $weightReal;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $packages;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("orderGET")
     */
    private $dateAdd;

    /**
     * @ORM\Column(type="datetime")
     * @Groups("orderGET")
     */
    private $dateUpd;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\OrderDetail", mappedBy="orderId", cascade={"persist", "remove"})
     * @Groups({"orderGetItem", "orderPostCustomer"})
     */
    private $orderDetails;

    /**
     * Order constructor.
     */
    public function __construct()
    {
        $this->dateAdd = new \DateTime();
        $this->dateUpd = 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 getPhone(): ?string
    {
        return $this->phone;
    }

    /**
     * @param string $phone
     * @return Order
     */
    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getAddress(): ?string
    {
        return $this->address;
    }

    /**
     * @param string $address
     * @return Order
     */
    public function setAddress(string $address): self
    {
        $this->address = $address;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getAddress1(): ?string
    {
        return $this->address1;
    }

    /**
     * @param string $address1
     * @return Order
     */
    public function setAddress1(string $address1): self
    {
        $this->address1 = $address1;

        return $this;
    }

    /**
     * @return int|null
     */
    public function getZipcode(): ?int
    {
        return $this->zipcode;
    }

    /**
     * @param int $zipcode
     * @return Order
     */
    public function setZipcode(int $zipcode): self
    {
        $this->zipcode = $zipcode;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getCity(): ?string
    {
        return $this->city;
    }

    /**
     * @param string $city
     * @return Order
     */
    public function setCity(string $city): self
    {
        $this->city = $city;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getCountry(): ?string
    {
        return $this->country;
    }

    /**
     * @param string $country
     * @return Order
     */
    public function setCountry(string $country): self
    {
        $this->country = $country;

        return $this;
    }

    /**
     * @return null|int
     */
    public function getIdOrderMerchant(): ?int
    {
        return $this->idOrderMerchant;
    }

    /**
     * @param int $idOrderMerchant
     * @return Order
     */
    public function setIdOrderMerchant(int $idOrderMerchant): self
    {
        $this->idOrderMerchant = $idOrderMerchant;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getUrlTracking(): ?string
    {
        return $this->urlTracking;
    }

    /**
     * @param string $urlTracking
     * @return Order
     */
    public function setUrlTracking(string $urlTracking): self
    {
        $this->urlTracking = $urlTracking;

        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 getIdTrackingAkaz(): ?string
    {
        return $this->idTrackingAkaz;
    }

    /**
     * @param string $idTrackingAkaz
     * @return Order
     */
    public function setIdTrackingAkaz(string $idTrackingAkaz): self
    {
        $this->idTrackingAkaz = $idTrackingAkaz;

        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 mixed
     */
    public function getLastHistory()
    {
        return $this->lastHistory;
    }

    /**
     * @param mixed $lastHistory
     */
    public function setLastHistory(OrderHistory $lastHistory): void
    {
        $this->lastHistory = $lastHistory;
    }

    /**
     * @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;
    }

    public function getDateAdd()
    {
        return $this->dateAdd;
    }

    /**
     * @return \DateTimeInterface|null
     */
    public function getDateUpd(): ?\DateTimeInterface
    {
        return $this->dateUpd;
    }

    /**
     * @param \DateTimeInterface $dateUpd
     * @return Order
     */
    public function setDateUpd(\DateTimeInterface $dateUpd): self
    {
        $this->dateUpd = $dateUpd;

        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;
    }

    /**
     * @return mixed
     */
    public function getPackages()
    {
        return $this->packages;
    }

    /**
     * @param mixed $packages
     */
    public function setPackages($packages): void
    {
        $this->packages = $packages;
    }
}

您可以在此处了解有关序列化程序的更多信息:

symfony:https ://symfony.com/doc/current/components/serializer.html api-platform:https ://api-platform.com/docs/core/serialization/

谢谢 ;)


推荐阅读