Answer the question
In order to leave comments, you need to log in
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';
}
}
Answer the question
In order to leave comments, you need to log in
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();
// ваш код
},
));
}
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 questionAsk a Question
731 491 924 answers to any question