0

I have two entities with a Many->Many relation , appli and service I want to create a form that would allow me to edit, add, or remove relations .

Those entities are linked as follow ..

Appli.php :

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Service", mappedBy="applis")
 */
private $services;

public function getService() {
    return $this->services;
}

Service.php :

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Appli", inversedBy="services")
 * @ORM\JoinTable(name="service_to_app",
 *      joinColumns={@ORM\JoinColumn(name="services", referencedColumnName="id_service")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="applis", referencedColumnName="id_apps")}
 *      )
 */
private $applis;

Notice that $services and $applis are arrayCollections .

Here is my formType :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $item = $builder->getData();

    $builder /* ... */

        ->add('serviceCol', CollectionType::class, array(
            'entry_type' => ServiceType::class,
            'entry_options' => array('label' => false,), 
            'property_path' => 'service',
            'allow_add' => true, 'allow_delete' => true,
        ));
}
public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => Appli::class,
    ));
}

and the serviceType :

class ServiceType extends AbstractType {

public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder
        ->add('service', EntityType::class, array(
            'class' => Service::class,
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('s')
                    ->orderBy('s.id_service', 'ASC');},
            'choice_label' => 'nom', 'property_path' => 'nom', 'label' => false));
}

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => Service::class,
    ));
}

}

My issue so far is to preselect relations, I do have the correct amount of fields , but it's only default value and does not show the existing relations . I could get to it without using the CollectionType field , but then , I would not be able to add / remove fields .

I cant fill the 'data' option since $item->getService() gives an arrayCollection , and I could not find a way to iterate through it during the Collection definition .

Is there any simple enough solution to this ?

Trabi
  • 1
  • 3

1 Answers1

0

So the fix was quite simple , the solution was on Symfony form - Access Entity inside child entry Type in a CollectionType It also made me read How to Dynamically Modify Forms Using Form Events with a renewed attention !

here s is my ServiceType formBuilder :

$builder->addEventListener(FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($builder)  {
            $form = $event->getForm();
            $service = $event->getData();
            if($service instanceof Service) {
                $form->add('service', EntityType::class, array(
                    'class' => Service::class,
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('s')
                            ->orderBy('s.id_service', 'ASC');},
                    'choice_label' => 'nom',
                    'property_path' => 'nom',
                    'label' => false,
                    'data' => $service ));
            }
        })
    ;
Trabi
  • 1
  • 3