getChildren():返回值必须是 Doctrine\Common\Collections\Collection 类型,返回的字符串

问题描述

在我的代码中,我实现了 OnetoMany 自引用关系。我已经在构造函数中初始化了数组集合。当我发送查询时,父项被添加到子项中。但是当我尝试在 children 或 parent 属性中返回一个对象时,我得到的不是一个对象而是一个数组集合字符串。错误如下:

App\Entity\Employee::getChildren():返回值必须是类型 Doctrine\Common\Collections\Collection,返回字符串

这是我的实体:

<?PHP

namespace App\Entity;

use App\Repository\EmployeeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=EmployeeRepository::class)
 */
class Employee
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

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

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

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

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

    /**
     * @ORM\Column(type="integer")
     */
    private $workExperience;

    /**
     * @ORM\Column(type="integer")
     */
    private $levelOfKNowledge;

    /**
     * @ORM\Column(nullable=true)
     * @ORM\OnetoMany(targetEntity=Employee::class,mappedBy="parent")
     */
    private $children;

    /**
     * @ORM\Column(nullable=true)
     * @ORM\ManyToOne(targetEntity=Employee::class,inversedBy="children")
     */
    private $parent;

    public function __construct()
    {
        $this->children = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(string $firstName): self
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(string $lastName): self
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getPosition(): ?string
    {
        return $this->position;
    }

    public function setPosition(string $position): self
    {
        $this->position = $position;

        return $this;
    }

    public function getPhoneNumber(): ?string
    {
        return $this->phoneNumber;
    }

    public function setPhoneNumber(string $phoneNumber): self
    {
        $this->phoneNumber = $phoneNumber;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getWorkExperience(): ?int
    {
        return $this->workExperience;
    }

    public function setWorkExperience(int $workExperience): self
    {
        $this->workExperience = $workExperience;

        return $this;
    }

    public function getLevelOfKNowledge(): ?int
    {
        return $this->levelOfKNowledge;
    }

    public function setLevelOfKNowledge(int $levelOfKNowledge): self
    {
        $this->levelOfKNowledge = $levelOfKNowledge;

        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }
    
    public function setParent(Employee $employee): void
    {
        $this->parent = $employee;
    }

    /**
     * @return Collection|Employee[]|null
     */
    public function getChildren(): Collection
    {
        dump($this->children);
        return $this->children;
    }


    public function __toString(): string
    {
        return $this->children;
    }

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

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

}

解决方法

问题出在这部分:

    /**
     * @ORM\Column(nullable=true)
     * @ORM\OneToMany(targetEntity=Employee::class,mappedBy="parent")
     */
    private $children;

因为您已将字段映射为列和关系。您不能将 @Column@OneToMany 一起使用,因为它甚至没有意义 - children 无论如何都不会反映在数据库中。