Symfony2,Doctrine Extensions Tree : Generating a “tree”-like dropdown Select list

后端 未结 1 1407
难免孤独
难免孤独 2021-02-01 11:04

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
         


        
1条回答
  •  情歌与酒
    2021-02-01 11:43

    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

    0 讨论(0)
提交回复
热议问题