2017-05-04 12 views
0

に現在のエンティティを設定します。 CvTypeは、私はsymfonyの2.8で動作し、私は2つのエンティティを持っている他の

class CvForm extends AbstractType 
{ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
    $builder 
      //.. 
      ->add('formations', CollectionType::class, array(

     'entry_type' => FormationCvForm::class, 
     'allow_add' => true, 
     'by_reference' => false, 

    ))    ; 

} 

FormationCvType

class FormationCvForm extends AbstractType 
{ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
    $builder->add('titre') 
      ->add('etablissement') 
      ->add('description') 

      ->add('dateDebut', DateType::class, array(
               'widget' => 'choice', 'translation_domain' => 'FOSUserBundle','data' => new \DateTime())) 
      ->add('dateFin', DateType::class, array(
               'widget' => 'choice', 
                )) 

      ; 

} 

コントローラ

public function createAction(Request $request) 
    { 
    $em = $this->getDoctrine()->getManager(); 
     $cv = new Cv(); 

$form = $this->createForm('Front\FrontBundle\Form\CvForm', $cv); 
$form->handleRequest($request); 

if ($form->isSubmitted() && $form->isValid()) { 
     $user=$this->get('security.context')->getToken()->getUser(); 
     $cv->setEtudiant($user); 
     $cv->setTelephone($user->getTel()); 
     $cv->setDateNaissance($user->getBirthday()); 
     $cv->setActif(false); 




     $em = $this->getDoctrine()->getManager(); 



     $em->persist($cv); 
     $em->flush(); 
     $formations=$cv->getFormations(); 
     $formation= array(); 
     foreach ($formation as $formations) { 
      $formation->setCv($cv->getId()); 
      $em->persist($formation); 
      $em->flush(); 

     } 




     return $this->redirectToRoute('cv_show', array('id' => $cv->getId())); 

    } 

return $this->render("FrontBundle:CV:createCv.html.twig", array(
     'form' => $form->createView(), 

    )); 

} 

私はFormationCvのテーブルでは、フォームを送信した場合、常にNullを得るという問題、 誰かが私を助けてください? $formation= array();を削除し、foreachの状態であなたの変数を切り替える

$formations=$cv->getFormations(); 
    $formation= array(); 
    foreach ($formation as $formations) { // You're looping over $formation here, which is an empty array as per the line before this 
     $formation->setCv($cv->getId()); 
     $em->persist($formation); 
     $em->flush(); 

    } 

foreach ($formations as $formation) {

+0

実際に何が転送されたかを確認するには、ブラウザのPOSTリクエストを見ましたか? – ccKep

+0

また、あなたのエンティティの注釈が適切に設定されている場合(例えば、inversedByとカスケード)、コントローラで最後の部分(foreach全体)を削除することができます。 – ccKep

+0

プラス:あなたのforeachは何をすべきですか?空の配列を初期化して、それをループします。 – ccKep

答えて

2

あなたのforeachのは、1回の反復を実行するつもりはありません。

代わりに/好ましくは、ブロック全体を取り除き、エンティティを設定して永続操作を設定し、逆側(mappedBy/inversedBy)について伝えることでdoctrineに作業を任せます。

+0

それは、うまくいきます、ありがとう –

関連する問題