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

Posted

技术标签:

【中文标题】getChildren():返回值必须是Doctrine\\Common\\Collections\\Collection类型,返回字符串【英文标题】:getChildren(): Return value must be of type Doctrine\Common\Collections\Collection, string returnedgetChildren():返回值必须是Doctrine\Common\Collections\Collection类型,返回字符串 【发布时间】:2021-07-29 00:05:33 【问题描述】:

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

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;
    

【问题讨论】:

您的 __toString 方法似乎存在问题,该方法必须返回一个字符串,但您返回的是一个集合 $this-&gt;children。此外,您的 getChildren 的返回类型为 Collection,但您的注释显示为 Collection 或 null,因此您的返回类型应为 ?Collection 另外,除非有充分的理由存储 parent_id,否则我会避免这样做。应该使用$object-&gt;getParent()-&gt;getId()来获取父id,避免重复存储数据。 【参考方案1】:

问题出在这部分:

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

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

【讨论】:

以上是关于getChildren():返回值必须是Doctrine\Common\Collections\Collection类型,返回字符串的主要内容,如果未能解决你的问题,请参考以下文章

PHP SPL RecursiveIterator::getChildren() 究竟返回了啥?

xml Doctr的Travis.xml(IBM DB2)

xml Doctr的Travis.xml(IBM DB2)

ZooKeeper Watch Java API浅析getChildren

学说 QueryBuilder 返回 QueryException

csharp 的GetChildren()