Symfony 2.8表单CollectionType字段很多很多

In my model jai an Entity Piece and its Remplacement,the relation is defined like this:

/**
  * @var string
  *
  * @ORM\Column(name="Reference", type="string", length=255)
  */
private $reference;

/**
 * Bidirectional 
 *
 * @ORM\ManyToMany(targetEntity="Remplacement", inversedBy="origine",cascade="all", orphanRemoval=true)
 * @ORM\JoinTable(name="piece_remplace",
 *   joinColumns={@ORM\JoinColumn(name="id_org", referencedColumnName="id")},
 *   inverseJoinColumns={@ORM\JoinColumn(name="id_gen", referencedColumnName="id")}
 * )
 */
protected $generique;

/**
 * @var string
 *
 * @ORM\Column(name="Reference_g", type="string", length=255)
 */
private $referenceG;

/**
 * Bidirectional 
 *
 * @ORM\ManyToMany(targetEntity="Piece", mappedBy="generique")
 */
protected $origine;

I work with CRUD symfony for Piece and its Replacement, from CollectionType http://symfony.com/doc/current/cookbook/form/form_collections.html Show Piece works well,And the remains are:

enter image description here

PieceType:

<?php

namespace STM\DevisBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class PieceType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('reference',TextType::class)
                ->add('type',TextType::class)
                ->add('marque',TextType::class)

                ->add('generique',CollectionType::class, array(
                             'entry_type' => RemplacementType::class,
                             'allow_add'  => true,
                            'allow_delete' => true))
                   
    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'STM\DevisBundle\Entity\Piece'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'stm_devisbundle_piece';
    }


}

RemplacementType:

<?php

namespace STM\DevisBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class RemplacementType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('referenceG',TextType::class)
                ->add('typeG',TextType::class)
                ->add('marqueG',TextType::class)
                ->add('origine',CollectionType::class, array(
                             'entry_type' => PieceType::class,
                            'allow_add'    => true,
                            'allow_delete' => true));

               
    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'STM\DevisBundle\Entity\Remplacement'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'stm_devisbundle_remplacement';
    }


}

Controller is symfony which generates

The but is to do CRUD with Piece and its Replacement. And I'm lost on Collection Type I need your help because it seems easy but I can not solve it

Thank you

</div>

Let me explain: you have 2 form types PieceType and RemplacementType

In the PieceType, you add a collection with entry type is RemplacementType. In the RemplacementType, you add a collection with entry type is PieceType.

Both of them allow_add and build the prototype forms. So:

  • The form builder build PieceType (*)
  • PieceType has a collection field, it create the prototype
  • The prototype use RemplacementType
  • The form builder build RemplacementType
  • RemplacementType has a collection field, it create the prototype
  • The prototype use PieceType
  • The form builder build PieceType (*)
  • ..and so on (circular calls -> infinity form level)

To avoid this case, one of them must disable adding the other one by add option: 'allow_add' => false, 'prototype' => false'