Symfony 4 API响应OneToMany关系为空数组

问题描述 投票:0回答:1

公司实体与地址实体具有OneToMany关系。 PUT和POST操作没有错误或错误。

当我向邮递员索取api请求时,“地址和公司Raletion是空响应。 (方法GET)

公司实体:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CompanyRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Company implements \JsonSerializable
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=150, nullable=true)
     */
    private $taxOffice;

    /**
     * @ORM\Column(type="string", length=15, nullable=true)
     */
    private $taxNumber;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isBranch;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $establishment;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="companies")
     */
    private $mainBranch;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Company", mappedBy="mainBranch")
     */
    private $companies;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Definition")
     * @ORM\JoinColumn(nullable=false)
     * @Assert\NotBlank()
     */
    private $companyType;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;

    /**
     * @ORM\Column(type="datetime")
     */
    private $updatedAt;

    /**
     * @ORM\Column(type="string", length=13, nullable=true)
     */
    private $telephone;

    /**
     * @ORM\Column(type="string", length=13, nullable=true)
     */
    private $mobilePhone;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $kepAddress;

    /**
     * @ORM\Column(type="string", length=13, nullable=true)
     */
    private $fax;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Person", mappedBy="company")
     */
    private $people;

    /**
     * @ORM\Column(type="string", length=150)
     */
    private $companyName;

    /**
     * @ORM\Column(type="string", length=200)
     */
    private $companyTitle;

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

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

    /**
     * @ORM\Column(type="string", length=25, nullable=true)
     */
    private $tradeRegisterNumber;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Address", mappedBy="company")
     */
    private $addresses;

    /**
     * Specify data which should be serialized to JSON
     * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
     * @return mixed data which can be serialized by <b>json_encode</b>,
     * which is a value of any type other than a resource.
     * @since 5.4.0
     */
    public function jsonSerialize()
    {
        return array(
            "id"                => $this->id,
            "mainBranch"        => $this->mainBranch,
            "companyName"       => $this->companyName,
            "companyTitle"      => $this->companyTitle,
            "taxOffice"         => $this->taxOffice,
            "taxNumber"         => $this->taxNumber,
            "isBranch"          => $this->isBranch,
            "companyType"       => $this->companyType,
            "telephone"         => $this->telephone,
            "mobilePhone"       => $this->mobilePhone,
            "kepAddress"        => $this->kepAddress,
            "fax"               => $this->fax,
            "addresses"         => $this->addresses,
            "companies"         => $this->companies,
            "establishment"     => null === $this->establishment ? '' : $this->establishment->format('Y-m-d H:i:s'),
            'createdAt'         => null === $this->createdAt ? '' : $this->createdAt->format('Y-m-d H:i:s'),
            'updatedAt'         => null === $this->updatedAt ? '' : $this->updatedAt->format('Y-m-d H:i:s')
        );
    }
}

和地址实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\AddressRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Address implements \JsonSerializable
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\City")
     * @ORM\JoinColumn(nullable=false)
     */
    private $city;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\District")
     * @ORM\JoinColumn(nullable=false)
     */
    private $district;

    /**
     * @ORM\Column(type="string", length=255)
     */
    // TODO eğer adres => address olursa POST işleminde this value is invalid hatası veriyor. Bunu araştıralım
    private $adres;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isDefault;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isInvoice;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $createdAt;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updatedAt;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="addresses")
     */
    private $company;

    /**
     * Specify data which should be serialized to JSON
     * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
     * @return mixed data which can be serialized by <b>json_encode</b>,
     * which is a value of any type other than a resource.
     * @since 5.4.0
     */
    public function jsonSerialize()
    {
        return array(
            "id"            => $this->id,
            "city"          => $this->city,
            "district"      => $this->district,
            "adres"         => $this->adres,
            "isDefault"     => $this->isDefault,
            "isInvoice"     => $this->isInvoice,
            'createdAt'     => null === $this->createdAt ? '' : $this->createdAt->format('Y-m-d H:i:s'),
            'updatedAt'     => null === $this->updatedAt ? '' : $this->updatedAt->format('Y-m-d H:i:s')
        );
    }
}

和API响应:

{
    "id": 1,
    "mainBranch": null,
    "companyName": "Mustapayev AŞ",
    "companyTitle": "Mustapayev İnşaat Müt. AŞ",
    "taxOffice": null,
    "taxNumber": null,
    "isBranch": false,
    "companyType": {
        "id": 8,
        "parentDefinition": {
            "id": 1,
            "parentDefinition": null,
            "title": "Kuruluş Tipi"
        },
        "title": "Anonim Şirket"
    },
    "telephone": null,
    "mobilePhone": null,
    "kepAddress": null,
    "fax": null,
    "addresses": {},
    "companies": {},
    "establishment": "",
    "createdAt": "2019-10-04 14:56:33",
    "updatedAt": "2019-10-04 14:56:33"
}
json symfony symfony4
1个回答
0
投票

您应该尝试添加->toArray(),例如:

public function jsonSerialize()
{
    return array(
        "companies" => $this->companies->toArray()
    );
}
© www.soinside.com 2019 - 2024. All rights reserved.