There is an entity that has a tree implemented by specifying the parent.

// ... class Folder { /** * @ORM \ManyToOne(targetEntity="Folder") * @ORM \JoinColumn(name="pid", referencedColumnName="id") */ protected $parent; // ... } 

The form is made like this:

 $builder->add('parent', 'entity', array( 'class' => 'MyBundle:Folder', )) 

In this case, the list of "Folders" turns out to be "flat", from a simple maximum that can be done to describe it in the query_builder form builder and sort it by some field ...

Required : display a list of "folders" in the tree view. for example, now the list of all folders will look something like this:

 Folder1 Folder2 Folder3 Folder4 

but should like this:

 Folder1 Folder4 Folder3 Folder2 

those. there will be the same select option, but in a special order in accordance with the parent-child hierarchy, also inside the option add a couple of spaces for visual indentation.

The second task is more complicated : it would be very convenient to say, when editing Folder4 in the list, Folder3 is displayed as inactive. you can’t ask your baby as a parent :) well, and the third question: how to check this moment while saving?

    1 answer 1

    I think it would be very difficult to set the logic for outputting or not outputting tree values ​​through Doctrine. You can bring to the model, get the prepared data from it and feed it to the builder as the 3 parameters of the createForm function (...). And what to deduce or not to deduce, this is what you yourself determine in the model, based on the application logic.

    In the controller

     ... $listEntity = $modelFolder->GetListForSelecet($data); $form = $this->createForm(new myType(), $entityFolder, array('listEntity' => $listEntity); ... 

    In the form class myType

     public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('idFolder', 'choice', array('label' => 'Папка', 'choices' => $options['listEntity'], 'expanded' => false, 'mapped' => false) ); } 

    still in myType set the parameter listEntity = null, and then it will swear

     public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Acme\AcmeBundle\Entity\Folder', 'listEntity' => null )); }