How to apply a model transformer to collection items in Symfony2 forms?

Context

I have a symfony2 form field of type collection , where the elements of the collection are of type entity . I am using Symfony 2.7.

Problem

So far this works, but in this case I have to apply a model data converter to these elements of the collection, as described in the Symfony book . I use this piece of code:

 <?php $builder ->add( $builder ->create('items', 'collection', array( 'type' => 'entity', 'options' => array( 'class' => 'AppBundle:Item', 'property' => 'name', 'label' => 'Item', ), 'label' => 'Items', 'allow_add' => true, 'allow_delete' => true, 'delete_empty' => true, 'prototype' => true, 'required' => false, )) // $options['em'] is the entity manager ->addModelTransformer(new ItemToNumberTransformer($options['em'])) ) ; 

Unfortunately, this applies to the model transformer for the entire collection, and not for a single Item element. As a workaround, I modified the transformer to work with arrays of / ids elements instead of a single / id element, but this view does not look different to handle this. It seems to me that this is more of a syntactic problem.

Question

Does anyone know how to apply model transformers to each element of a collection? Or will someone confirm that this is simply not possible due to a limitation within Symfony?

+6
source share
2 answers

I would say, instead of creating collection entity types, you need to create your own type.

 namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Doctrine\ORM\EntityManager; /* Other use statements */ class ItemEntityType extends AbstractType { /** * @var \Doctrine\ORM\EntityManager */ protected $em; public function __construct(EntityManager $em) { $this->em = $em } public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addModelTransformer(new ItemToNumberTransformer($this->em)); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'class' => 'AppBundle:Item', 'property' => 'name', 'label' => 'Item', )); } public function getParent() { return 'entity'; } public function getName() { return 'appbundle_item_entity'; } } 

Then define it as a service

application /Config/services.yml

 services: form.type.model.item_entity: class: AppBundle\Form\Type\ItemEntityType arguments: ["@doctrine.orm.entity_manager"] tags: - {name: form.type, alias: appbundle_item_entity} 

And now you can specify this as the type of your collection

 $builder ->create('items', 'collection', array( 'type' => 'appbundle_item_entity' 'label' => 'Items', 'allow_add' => true, 'allow_delete' => true, 'delete_empty' => true, 'prototype' => true, 'required' => false, )) 

Disclosure: I have not tested this, but it should work.

+7
source

You should create a Type for the Item, apply a transformer to it, and then use it as a type for your collection.

+2
source

Source: https://habr.com/ru/post/988165/


All Articles