Symfony2,Doctrine Extensions Tree : Generating a “

2020-05-19 03:10发布

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

and visually like this:

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:

enter image description here

how can i configure my menu settings, so it generates a Tree-Like-dropdown select like this?

enter image description here

1条回答
仙女界的扛把子
2楼-- · 2020-05-19 04:03

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 :

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');
},

Then, you need to add a getIndentedTitle method to your entity:

public function getIndentedTitle() {
    return str_repeat("--", $this->lvl).$this->title;
}

Finally, add a property option to your options when you build the form, to display the virtual property indentedTitle instead of title :

'property' => 'indentedTitle'

See : http://symfony.com/doc/current/reference/forms/types/entity.html

查看更多
登录 后发表回答