I have a self referencing entity in Doctrine but I keep getting the following error when I try to persist them:
PHP Catchable fatal error: Object of class Category could not be converted to string in vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php on line 165
Example:
$category1 = new Category();
$category1->setName("Foo");
$category1->setParent( NULL );
$category2 = new Category();
$category2->setName("Bar");
$category2->setParent( $category1 );
$manager->persist( $category1 );
$manager->persist( $category2 );
$manager->flush();
My entity looks like this:
/**
* @ORM\Table(name="categories")
* @ORM\Entity
*/
class Category {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=64, unique=true)
*/
protected $name;
/**
* @ORM\Column(nullable=true)
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
protected $children;
public function __construct()
{
$this->children = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName( $name )
{
$this->name = $name;
return $this;
}
public function getParent()
{
return $this->parent;
}
public function setParent( Category $parent = NULL )
{
$this->parent = $parent;
return $this;
}
public function getChildren()
{
return $this->children;
}
public function setChildren( ArrayCollection $children )
{
$this->children = $children;
return $this;
}
}
I have Googled and compared my code to other examples but I can't seem to spot the problem. I'm obviously overlooking something, but what?