A
A
Alexander Vitkalov2017-08-08 15:08:38
MySQL
Alexander Vitkalov, 2017-08-08 15:08:38

How to remove one of the elements in the form combobox?

Greetings.
There is an entity

class Category
{
   ... 
    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var Category
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children", cascade={"persist"})
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
     */
    private $parent;
   ...

There is a form.
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, [
                'label' => "Категория",
            ])
            ->add('parent', EntityType::class, [
                'label' => "Родительская категория",
                "class" => Category::class,
                "choice_label" => "name",
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('c')
                        ->where('c.parent != :category')
                        ->setParameter('category', ???);
                }
            ]);
    }

There is an edit action.
public function editAction(Request $request, Category $category)
    {
        $editForm = $this->createForm('MyBundle\Form\CategoryType', $category);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            ...
        }

        return $this->render...
    }

Actually everything is standard. When editing a category, you can specify a parent category. But the editable category also appears in this list. How to specify the current (edited category) in query_builder to exclude it from the selection list?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2017-08-08
@nechin

Try it in the form:
QueryBuilder:

'query_builder' => function(EntityRepository $er) use ($entity) {
                    return $er->createQueryBuilder('c')
                        ->where('c.parent != :category')
                        ->setParameter('category', $entity);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question