Php 条令父级子级查找无效的Foreach

Php 条令父级子级查找无效的Foreach,php,doctrine-orm,associations,parent-child,Php,Doctrine Orm,Associations,Parent Child,我只是想建立一个简单的亲子关系,但我显然做错了什么。我正在关注该理论网站上的内容 我收到以下错误: 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

我只是想建立一个简单的亲子关系,但我显然做错了什么。我正在关注该理论网站上的内容

我收到以下错误:

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

我自己似乎找不到,但PHP中的名称空间是否区分大小写

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


希望这有帮助

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

您可以使用:

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

很棒的想法。名称空间的大小写没有引起问题。据我所知,这没有任何区别,我的调用页确实输出了正确的id。对于构造函数,这里的答案是:find不调用uu构造。如果在加载和初始化集合后调用_构造,错误会消失,但会丢失所有预期的关系。