按照本教程并将所有内容放在一起使其在我的项目中工作,只是为了显示一个嵌套列表(使用学说 2 和 zf2),我无法进入 foreach。使用这段代码:
$root_categories = $em->getRepository('Controleitor\Model\Entity\Category')->findBy(array('parent_category' => null));
$collection = new \Doctrine\Common\Collections\ArrayCollection($root_categories);
$category_iterator = new \MYMODULE\Model\Entity\RecursiveCategoryIterator($collection);
$recursive_iterator = new \RecursiveIteratorIterator( $category_iterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursive_iterator as $index => $child_category){
echo 'test';
}
Debug::dump($recursive_iterator);die;
我期待打印“测试”字符串,但它只打印:
object(RecursiveIteratorIterator)#414 (0) {}
但是当我在转储之前这样做时:
$recursive_iterator->current()->getTitle();
我得到了标题.. 它以某种方式循环 \Doctrine\Common\Collections\ArrayCollection 对象失败。