V
V
Valery2013-07-11 15:58:36
symfony
Valery, 2013-07-11 15:58:36

How to get ID in QueryBuilder inside FormType?

Good evening,

There is a FormType on Symfony 2.3

namespace Q\FilesBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

use Q\FilesBundle\Entity\File;


class FilesType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'multiple' => true,
            'required' => false,
            'class' => 'QFilesBundle:File',
            'query_builder' => function($repository) {
                        // Вот тут
            },
        ));
    }

    public function getParent()
    {
        return 'entity';
    }

    public function getName()
    {
        return 'files';
    }
}


How to get the ID of the current entity for which the form is being built inside the closure (query_builder)?
Simply put, I need to filter this list by parent ID
. Only custom FormType can be manipulated.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zloyusr, 2013-07-12
@zloyusr

Just pass the desired entity to your type constructor and then something like:

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'multiple' => true,
            'required' => false,
            'class' => 'QFilesBundle:File',
            'query_builder' => function($repository) {
                        $id = $this->news->getId();
                       // ваш код
            },
        ));
    }

T
to0n1, 2013-07-19
@to0n1

You need to use EventListeners for the form on the PRE_BIND events (if I'm not mistaken in 2.3 PRE_SUBMIT) and on the PRE_SET
code of one open source project (I have nothing to do with it, don't mistake it for advertising)
By the link you will see how the list of provinces is dynamically built in depending on the selected country.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question