user1970395 user1970395 - 1 year ago 197
PHP Question

Extend Doctrine2 form EntityType

I'm looking for a way to extend Symfony 2 EntityType [Symfony\Bridge\Doctrine\Form\Type\EntityType] - as in a new type extending this one, not creating a FormTypeExtension - and I can't figure it out. Does anyone know any proper way to do that?

I've tried simply extending it that way:

class NestedEntityType extends EntityType {

public function getName() {
return $this->getBlockPrefix();

public function getBlockPrefix() {
return 'nested_entity';

and then in sonata admin class I have:

protected function configureFormFields(FormMapper $formMapper)
$formMapper->add('types', NestedEntityType::class, [
'label' => false,
'multiple' => true,
'expanded' => true,
'by_reference' => false

but unfortunately it causes Fatal Error:

Catchable Fatal Error: Argument 1 passed to
Symfony\Bridge\Doctrine\Form\Type\DoctrineType::__construct() must
implement interface Doctrine\Common\Persistence\ManagerRegistry, none
given, called in

I need to keep the whole functionality of EntityType, with one exception - the way it's presented. That's why I need to extend this type (I use it in other fields, so I can't just modify the template for it!).

I'm using Symfony 2.8 (just for the record).

Answer Source

So, if you need a create a reusable solution, for different Entities, you don't need a configureOptions in this case.

You need to create an elementType in you code like

$this->createForm(NestedEntityType::class, null, ['class' => YourEntity::class]);

And in this case, you will need to pass as an option a name of class Entity which is nested.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download