【发布时间】:2014-01-28 08:02:32
【问题描述】:
我在 Doctrine 中有一个自引用实体,但是当我尝试持久化它们时,我不断收到以下错误:
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
例子:
$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();
我的实体如下所示:
/**
* @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;
}
}
我在 Google 上搜索并将我的代码与其他示例进行了比较,但我似乎无法发现问题所在。我显然忽略了一些东西,但是什么?
【问题讨论】:
标签: doctrine-orm entity-relationship self-reference