【问题标题】:Symfony2.8 form CollectionType Field manyTo manySymfony2.8 表单 CollectionType 字段 manyTo many
【发布时间】:2017-08-31 09:32:45
【问题描述】:

在我的模型中,一个实体片及其替换,关系定义如下:

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

我使用 CRUD symfony 为 Piece 及其替代品工作,来自 CollectionType http://symfony.com/doc/current/cookbook/form/form_collections.html Show Piece 效果不错,剩下的有:

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


}

替换类型:

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


}

控制器是 symfony 生成的

但是是用 Piece 和它的 Replacement 来做 CRUD。我迷失在集合类型上 我需要你的帮助,因为这看起来很简单,但我无法解决它

谢谢

【问题讨论】:

  • 那么你的问题到底是什么?尝试编辑您的问题,以更清楚地了解您想要实现的目标,以及为什么它不起作用。
  • 谢谢,这不起作用:我无法修改和删除(CRUD)片段及其替换,编辑和删除不起作用,我想我认为问题出在标准 collectionType 上。我跟着symfony.com/doc/current/cookbook/form/form_collections.html 但同样的错误

标签: php symfony


【解决方案1】:

让我解释一下:您有两种表单类型PieceType 和RemplacementType

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

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

  • 表单构建器构建PieceType (*)
  • PieceType 有一个集合字段,它创建原型
  • 原型使用RemplacementType
  • 表单生成器构建RemplacementType
  • RemplacementType 有一个集合字段,它创建原型
  • 原型使用PieceType
  • 表单构建器构建PieceType (*)
  • ..等等(循环调用->无穷形式级)

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

【讨论】:

  • 谢谢猪球,没错,你明白,但它还不起作用,当我编辑时页面什么都不显示(空白页面)
  • 这取决于您的控制器、您的视图以及您在视图中呈现表单的方式。给我看看你的控制器和你的视图,希望我能看到问题。
  • 猪球,谢谢你看到控制器编辑和查看上面的编辑,问题是我无法显示编辑视图
猜你喜欢
  • 2017-01-15
  • 2016-10-12
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
相关资源
最近更新 更多