Symfony:选择字段中的选定选项始终为NULL

时间:2017-03-27 21:59:35

标签: symfony

Pleeeeeaaaase帮助我,我在这个问题堆叠,我没有太多时间,我是symfony的新手,并且自己无法解决这个问题! :( 我有一个包含动态选择字段的表单,当提交时我收到错误:

An exception occurred while executing 'INSERT INTO userprof (id_profil) VALUES (?)' with params [null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id_profil' cannot be null

因此问题出在所选择的$lib_userprof=$request->request->get('profils');$idProf=$rep->findOneBy($lib_userprof);

以下是代码:

$user = new user();
        $profils=$rep->findAll();

        $j=0;
        $profArr[]=array();
        foreach ($profils as $p) {
            $libelle= $p->getLibelle();

            $profArr[$j]=$libelle;
            $j=$j+1;

        }
        $form2 = $this->createFormBuilder($user,array('csrf_protection' => false))
            ->add('id', 'text',array('attr'=>array("autocomplete" => "off",'name'=>'login_user','required'=>'required',
                'maxlength'=>'255','placeholder'=>'Matricule')))
            ->add('password', 'password',array('attr'=>array('autocomplete' => 'off','placeholder'=>'Mot de passe','required'=>'required')))
            ->add('profils', 'choice'
                ,array( 'choices' => array('Profils' => $profArr),'mapped'=>false),
                array('attr'=>array('required'=>'required')))
            ->add('Ajouter', 'submit', array('attr' => array('class' => 'btn btn-primary btn-block rounded_btn', 'id' => 'login_btn',
                'style' => "width:6vw;height:5vh;padding:0px 0px; position:relative;left:5vmin;top:1vmin;font-size:2vmin;")))
            ->getForm();


        $form2->handleRequest($request);
        $id = $request->request->get('id');
        $pwd= $request->request->get('password');
        $lib_userprof=$request->request->get('profils');
        $userprof=new userprof();
        if ($form2->isSubmitted() && $form2->isValid()) {
            $em=$this
                ->getDoctrine()
                ->getManager();
            //$lib_userprof=$request->request->get('profils');
            $idProf=$rep->findOneBy($lib_userprof);
            $id=$request->request->get('id');
            //$userprof->setId($id);
            //$userprof->setIdProfil($id_userprof);
            $em->persist($userprof);
            $em->flush();


            $user->setId($id);
            $user->setPassword($pwd);

            $user->setEtat(true);
            //$em->merge($user);
            $em->persist($user);
            $em->flush();
            return $this->redirectToRoute('userProf', array('users'=>$users,
                'form2'=>$form2->createView()
            ));
var_dump($lib_userprof);
        }

0 个答案:

没有答案