我在 Symfony2 项目中实现了 Gedmo 嵌套树,并且正在尝试构建父选择。我阅读了文档https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/tree.md 但这是关于此类问题的非常简短的说明。
我无法获得具有基于节点级别标识的节点数组。
$repo = $em->getRepository('SymdruMenuBundle:MenuLink');
$options = array(
'decorate' => true,
'nodeDecorator' => function($node) {
return str_repeat(' ', $node['lvl']).'-'.$node['title'].'</a>';
},
'html' => false,
);
$htmlTree = $repo->childrenHierarchy(
null, /* starting from root nodes */
false, /* true: load all children, false: only direct */
$options
);
var_dump($htmlTree);
$form->add('parent', 'choice', array('choices' => $htmlTree));
它给了我一个字符串而不是一个数组。
我可以这样做
$em = $this->getDoctrine()->getManager();
$links = $em->getRepository('SymdruMenuBundle:MenuLink')->findAll();
$choices = array();
foreach ($links as $link) {
$choices[$link->getId()] = str_repeat('--', $link->getLvl()).$link->getTitle();
}
但这是最好的方法吗?
这里的第二个问题是当我将子对象保存到数据库时如何获取父对象?