Reputation: 101
I have simple form generator field like this:
$formMapper->add('project',EntityType::class, [
'class' => Project::class,
]);
It is field for select parent in tree data structure. It works very well in ADD, but in Edit i dont want to project with id X show as possible to select parent for project with id X
I am trying to use 'query_builder' property, but dont know how to catch id of current editing item from Admin class.
How to catch this id or simplest exclusion id of current editing item in select?
Upvotes: 0
Views: 1263
Reputation: 1507
I suppose your FormType is mapped on your edited item. Therefore, you could go with something like (class / fields / entity names to be replaced by yours) :
use Doctrine\ORM\EntityRepository;
class ProjectFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$currentId = $builder->getData()->getId();
$builder->add('project', EntityType::class, array(
'class' => Project::class,
'query_builder' => function (EntityRepository $er) use ($currentId) {
return $er->createQueryBuilder('p')
->where('p.id != :idCurrent')
->setParameter('idCurrent', $currentId);
},
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Project::class,
));
}
}
Upvotes: 2