在我的模型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运作良好,剩下的就是:
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;
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';
}
}
&#13;
Controller是生成
的symfony但是要使用Piece及其替换进行CRUD。而且我在收集类型上输了 我需要你的帮助,因为它似乎很容易,但我无法解决它
谢谢
答案 0 :(得分:0)
让我解释一下:您有两种表单类型PieceType
和RemplacementType
在PieceType
中,添加条目类型为RemplacementType
的集合。
在RemplacementType
中,添加条目类型为PieceType
的集合。
他们两个allow_add
并构建原型表单。所以:
PieceType
(*) PieceType
有一个收集字段,它创建原型RemplacementType
RemplacementType
RemplacementType
有一个收集字段,它创建原型PieceType
PieceType
(*) 为了避免这种情况,其中一个必须通过添加选项禁用添加另一个:'allow_add' => false, 'prototype' => false'