Symfony2,教义扩展树:生成“树”类下拉列表选择列表 [英] Symfony2,Doctrine Extensions Tree : Generating a "tree"-like dropdown Select list
问题描述
我有一个使用Tree架构构建的Categories表,使用Doctrine Tree Extension
,它看起来像这样
I have a Categories table, built with Tree architecture, using Doctrine Tree Extension and it looks something like this
id parent_id title lft lvl rgt root
864 (NULL) Movies 1 0 18 864
865 864 Packs 2 1 3 864
866 864 Dubbed 4 1 5 864
,视觉上像这样:
Movies
|
|
|->Packs
|->Dubbed
生成表单添加评论,并加载类别作为每个电影的下拉列表,所以我在我的电影审查表单类型
now i want to generated form for adding reviews , and loading categories as dropdown list for each movie, so i have in my movie-review form-type-class
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name');
$builder->add('file');
$builder->add('cover');
$builder->add('category','entity', array('class'=>'Tracker\MembersBundle\Entity\Category', 'property'=>'title', ));
}
它生成一个普通的下拉菜单,如下所示:
which generates a normal dropdown menu like this:
如何配置我的菜单设置,因此它会像这样生成一个 Tree-Like-dropdown选择?
how can i configure my menu settings, so it generates a Tree-Like-dropdown select like this?
推荐答案
我不知道这是一个好主意:用户将无法输入他们的选择。
I'm not sure this is a good idea : users won't be able to type in their choice.
尚未测试此解决方案,但它应该工作:
Haven't tested this solution, but it should work :
首先,您可以按root和lft值对三个进行排序,以正确显示它,因此添加一个查询构建器:
First, you can sort the three by root and lft value to display it properly, so add a query builder:
'query_builder' => function($er) {
return $er->createQueryBuilder('c')
->orderBy('c.root', 'ASC')
->addOrderBy('c.lft', 'ASC');
},
然后,您需要添加一个 getIndentedTitle
方法到您的实体:
Then, you need to add a getIndentedTitle
method to your entity:
public function getIndentedTitle() {
return str_repeat("--", $this->lvl).$this->title;
}
最后,在构建表单时,为您的选项添加一个属性选项,显示虚拟属性indentedTitle而不是标题:
Finally, add a property option to your options when you build the form, to display the virtual property indentedTitle instead of title :
'property' => 'indentedTitle'
请参阅: http://symfony.com/doc/current/reference/forms/types/entity.html
这篇关于Symfony2,教义扩展树:生成“树”类下拉列表选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!