2017-06-15 148 views
0

我有一個表單來修改一個實體,它有一些子實體。我使用表單集合來做到這一點。當我編輯這個實體時,應該顯示子集合,但它不會。子集合由2個選擇字段和1個整數字段組成。整數字段使用正確的數據很好地呈現,但選擇字段要求選擇一個選項,而它應該顯示Matiere和Colle的子實體。選擇字段的表單集合

在代碼中,Colle實體是Matiere實體的子代。我使用MaterializeCSS作爲框架。

這裏是我的代碼:

子窗體:

$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('colleEnfant', 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]); 

父窗體:

$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]); 

查看:

<table id="tableau" class="creneaux" 
       data-prototype="{{ form_widget(form.collesEnfants.vars.prototype)|e }}"> 

    <thead> 
     <tr> 
      <th>Matière</th> 
      <th>Colle</th> 
      <th>Ordre</th> 
      <th>Supprimer</th> 
     </tr> 
    </thead> 

    <tbody> 
     {% for colle in form.collesEnfants %} 
      <tr> 
      <td>{{ form_row(colle.matiere) }}</td> 
      <td>{{ form_row(colle.colleEnfant) }}</td> 
      <td>{{ form_row(colle.ordre) }}</td> 
      <td><a href="" class="delete_colle_link"><i class="material-icons">delete</i></a></td> 
      </tr> 
     {% endfor %} 
    </tbody> 
</table> 

<script> 
    $(document).ready(function() { 
     $('.matiere').material_select(); 
     $('.colles').material_select() 
    }); 
</script> 
+0

場渲染是好的?你只是想知道爲什麼現有的數據沒有在選擇框中選擇? – Jeet

+0

字段渲染是可以的。正如你所說的,唯一的問題是選擇字段中沒有選擇的現有數據 –

回答

0

我最終用JQuery來做。

腳本:

$(document).ready(function() { 
    {% for colleEnfant in collesEnfants %} 
     $('#paces_colle_colle_ajoutsupercolle_collesEnfants_{{ loop.index0 }}_matiere option[value="{{ colleEnfant.matiere }}"]').prop('selected', true); 
     $('#paces_colle_colle_ajoutsupercolle_collesEnfants_{{ loop.index0 }}_colleEnfant option[value="{{ colleEnfant.colle }}"]').prop('selected', true); 
    {% endfor %} 
    $('.matiere').material_select(); 
    $('.colles').material_select(); 
} 

控制器:

$collesEnfantsForm = []; 
foreach ($colle->getCollesEnfants() as $colleEnfant) { 
    $collesEnfantsForm[] = ['matiere' => $colleEnfant->getMatiere()->getId(), 'colle' => $colleEnfant->getId()]; 
} 
0

你可以在你的實體類型添加查詢並檢查領域'nom'ex在你的實體中。

$builder->add('colleEnfant', EntityType::class, [ 
    'class'  => 'PACESColleBundle:Colle', 
    'attr'   => ['class' => 'colles'], 
    'choice_label' => 'nom', 
    'label'  => false, 
    'required'  => true, 
    'placeholder' => 'choose.colle', 
    'query_builder' => function(ColleRepository $repository) { 
     $qb = $repository->createQueryBuilder('c'); 
     // the function returns a QueryBuilder object 
     return $qb->groupBy('c.matiere')->orderBy('c.nom', 'ASC'); 
    } 
]) 

當您需要添加檢測選擇修改的事件偵聽器之後。

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { 
    $data = $event->getData(); 
    $matiere = $this->em->getRepository('PACESColleBundle:Matiere')->find($data['matiere']); 
    $form = $event->getForm(); 

    $form->add('colleEnfant', EntityType::class, [ 
     'class'  => 'PACESColleBundle:Colle', 
     'attr'   => ['class' => 'colles'], 
     'choice_label' => 'nom', 
     'label'  => false, 
     'required'  => true, 
     'placeholder' => 'choose.colle', 
     'query_builder' => function(ColleRepository $repository) use ($matiere) { 
      $qb = $repository->createQueryBuilder('c'); 
      // the function returns a QueryBuilder object 
      return $qb 
       ->where('c.matiere = :matiere') 
       ->setParameter('matiere', $matiere) 
       ->orderBy('c.nom', 'ASC'); 
     } 
    ]); 
}); 

我希望這會幫助你。

+0

實際上,我在我選擇的字段中有數據,這些數據都是由Matiere分組的所有Colle對象。我想要的是讓所有已經在兒童收藏中的Colle在選擇字段中進行選擇。 –

+0

Matiere和Colle有一對多的關係嗎? – Mirouf

+0

是的。 Colle Entity中的自引用OneToMany關係 –

相關問題