我只是想用 Doctrine 2 建立一个简单的亲子关系,但我显然做错了什么。我正在关注Doctrine 网站的关联映射页面上所说的内容。
我收到以下错误:
Warning: Invalid argument supplied for foreach() in .../Doctrine/ORM/Persisters/BasicEntityPersister.php on line 1579
Fatal error: Call to a member function setValue() on a non-object in .../Doctrine/ORM/PersistentCollection.php on line 175
这是我的数据库设置:
CREATE TABLE IF NOT EXISTS `foo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
这是我的课:
namespace Classes;
/** @Entity @Table(name="foo") */
class Foo
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @OneToMany(targetEntity="classes\Foo", mappedBy="parent_id")
**/
private $children;
/** @ManyToOne(targetEntity="classes\Foo", inversedBy="children")
* @JoinColumn(name="parent_id", referencedColumnName="id")
**/
private $parent;
public function __get($name)
{
if(property_exists($this, $name)){
return $this->$name;
} else {
throw new \Exception('Field "' . $name . '" does not exist.');
}
}
}
这是调用它的页面:
$server = '/Applications/XAMPP/htdocs/projects/Scratch';
require_once $server . '/Model/bootstrap.php';
$myFoo = $em->find('classes\Foo',37);
echo $myFoo->id . '<br />';
echo count($myFoo->children);