In my code, I have implemented OneToMany self-referencing relationship. I have initialized array collection in the constructor. When I send a query parent is added to the children. But when I try to return an object in the children or parent property I get not an Object but an Array collection string. The error looks like this:
App\Entity\Employee::getChildren(): Return value must be of type Doctrine\Common\Collections\Collection, string returned
Here is my entity:
<?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;
}
}
The problem lies in this part:
/**
* @ORM\Column(nullable=true)
* @ORM\OneToMany(targetEntity=Employee::class, mappedBy="parent")
*/
private $children;
as you've mapped your field both as a column and a relation. You must not use @Column
along with @OneToMany
as it's not even making sens - the children
is not reflected in the database anyway.