创建的实体而不是CollectionType中的链接

时间:2017-08-30 17:41:57

标签: symfony symfony-forms

我的某个表单有问题。表单必须创建一个新的Colle实体并将其他一些Colle实体链接到它。 当我提交它时,会为collesEnfants集合字段中的每个项目创建一个新实体。创建的新实体已正确链接到父级,并且具有正确的“ordre”功能。字段,但它是一个新创建的实体,而不是我选择的实体。

我的表格:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('nom', TextType::class,['label' => 'Nom de la colle'])
    ->add('collesEnfants', CollectionType::class,
        ['label' => false,
        'entry_type' => SousColleFormType::class,
        'required' => true,
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false]);
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => 'PACES\ColleBundle\Entity\Colle'
    ]);
}

SousColleFormType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('matiere', EntityType::class, [
            'class' => 'PACESColleBundle:Matiere',
            'attr' => ['class'=> 'matiere'],
            'choice_label' => 'name',
            'label' => false,
            'required' => false,
            'placeholder' => 'Choisissez une matière',
            'mapped' => false])
        ->add('nom', EntityType::class, [
            'class' => 'PACESColleBundle:Colle',
            'attr' => ['class' => 'colles'],
            'choice_label' => 'nom',
            'label' => false,
            'group_by' => 'matiere',
            'required' => true,
            'placeholder' => 'choose.colle'])
        ->add('ordre', IntegerType::class,[
            'attr'=>['class'=>'ordre'],
            'required' => true,
            'label' => false]);
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => 'PACES\ColleBundle\Entity\Colle'
    ]);
}

控制器:

$formColleMere = $this->createForm(AjoutSuperColleFormType::class, $colle);
    $formColleMere->add('submit', SubmitType::class, ['label' => 'Créer']);

    $formColleMere->handleRequest($request);
    if ($formColleMere->isSubmitted() && $formColleMere->isValid()) {

        $collesEnfants = $formColleMere->get('collesEnfants')->getData();
        foreach ($collesEnfants as $enfant) {
            $colle->addColleEnfant($enfant);
        }

        if (!$colle->getCollesEnfants()->isEmpty()) {
            $em->persist($colle);
            $em->flush();
        }

Colle实体:

class Colle
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\OneToMany(targetEntity="Colle", mappedBy="colleMere", cascade={"persist"})
 * @ORM\OrderBy({"ordre" = "asc"})
 */
private $collesEnfants;

/**
 * @ORM\ManyToOne(targetEntity="Colle", inversedBy="collesEnfants", cascade={"persist"})
 * @ORM\JoinColumn(name="colleMere_id", referencedColumnName="id")
 */
private $colleMere;

/**
 * @var string
 *
 * @ORM\Column(name="nom", type="string", length=255)
 */
protected $nom;

{........}

/**
 * @ORM\ManyToOne(targetEntity="PACES\ColleBundle\Entity\Matiere", inversedBy="colles", cascade={"persist"})
 * @ORM\JoinColumn(name="matiere_id", referencedColumnName="id")
 * @ORM\OrderBy({"name" = "ASC"})
 */
protected $matiere;

/**
 * Cet attribut sert aux 'super colles' qui sont le résultat d'une fusion de colles d'une même UE
 * @var integer
 * @ORM\Column(name="ordre", type="integer", nullable=true)
 */
protected $ordre;

1 个答案:

答案 0 :(得分:0)

通过将'mapped' => false添加到collesEnfants字段,我成功地做了我想要的事情。 我还在Controller中更改了这些行:

$collesEnfants = $formColleMere->get('collesEnfants')->getData();
foreach ($collesEnfants as $enfant) {
   $colle->addColleEnfant($enfant);
}

致:

$collesEnfants = $formColleMere->get('collesEnfants')->getData();

foreach ($collesEnfants as $enfant) {
    $colle->addColleEnfant($enfant['nom']);
    $enfant['nom']->setOrdre($enfant['ordre']);
}