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

getChildren(): Return value must be of type Doctrine\Common\Collections\Collection, string returned

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

App\Entity\Employee::getChildren(): Return value must be of type Doctrine\Common\Collections\Collection, string returned

这是我的实体:

<?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 无论如何都不会反映在数据库中。