Doctrine Parent Child查找无效的Foreach

时间:2013-04-02 08:15:18

标签: php doctrine-orm associations parent-child

我只是想与Doctrine 2建立一个简单的亲子关系,但我显然做错了什么。我正在关注Doctrine网站Association Mapping page上的内容。

我收到以下错误:

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);

2 个答案:

答案 0 :(得分:0)

我自己似乎无法找到这个,但PHP中的命名空间区分大小写?

我在设置这些关系时看到的另一件事是在实体构造函数中实例化集合。这将特别适用于儿童财产。

希望这会有所帮助。

答案 1 :(得分:0)

虽然不是答案,但这是一个解决方法......

您可以使用:

$children = $em->getRepository('classes\Foo')->findby(array('parent_id',parentID);
相关问题