Symfony2.8表单CollectionType字段很多

时间:2017-04-05 16:28:37

标签: php symfony

在我的模型jai an Entity Piece及其Remplacement中,关系定义如下:

/**
  * @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;

我使用来自CollectionType的CRUD symfony for Piece及其替换 http://symfony.com/doc/current/cookbook/form/form_collections.html Show Piece运作良好,剩下的就是:

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';
    }


}
&#13;
&#13;
&#13;

RemplacementType:

&#13;
&#13;
<?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';
    }


}
&#13;
&#13;
&#13;

Controller是生成

的symfony

但是要使用Piece及其替换进行CRUD。而且我在收集类型上输了 我需要你的帮助,因为它似乎很容易,但我无法解决它

谢谢

1 个答案:

答案 0 :(得分:0)

让我解释一下:您有两种表单类型PieceTypeRemplacementType

PieceType中,添加条目类型为RemplacementType的集合。 在RemplacementType中,添加条目类型为PieceType的集合。

他们两个allow_add并构建原型表单。所以:

  • 表单构建器构建PieceType (*)
  • PieceType有一个收集字段,它创建原型
  • 原型使用RemplacementType
  • 表单构建器构建RemplacementType
  • RemplacementType有一个收集字段,它创建原型
  • 原型使用PieceType
  • 表单构建器构建PieceType (*)
  • ..依此类推(循环调用 - >无限形式级别)

为了避免这种情况,其中一个必须通过添加选项禁用添加另一个:'allow_add' => false, 'prototype' => false'