Symfony:如何使用另一个entilty的选择字段创建表单?

时间:2017-03-27 01:22:23

标签: php symfony

我有一个表单必须包含一个选项字段,该字段显示来自另一个实体(不是表单中使用的实体)的内容,所以这就是我尝试创建它的方式:

$user = new user();
        $profils=$rep->findAll();
        $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',[
                 'choices' => [$profils]],array('attr'=>array('mapped'=>false,'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();

Pleaaaase帮助我,因为我是symfony的新手,我可能会陷入你可能认为愚蠢的问题,我很抱歉,但我自己找不到解决方案。

2 个答案:

答案 0 :(得分:1)

我通过更改“'mapped'=> false”的位置来修复它,它起作用了:

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

答案 1 :(得分:0)

这样做的一种方法是:

在profil实体的存储库中创建一个方法,例如返回一个profils数组,例如

class ProfilRepository extends EntityRepository
{
    public function myFindAll()
    {
        $queryBuilder = $this->createQueryBuilder('p');
        $entites      = $queryBuilder->getQuery()->getArrayResult();

        return $entites;
    }
}

在你正在做的控制器中,

$user = new user();

// I'm asuming that Profil is an entity
$profils = $this
    ->getDoctrine()
    ->getManager()
    // this should return an array
    ->getRepository('SdzBlogBundle:Profil')->myFindAll()
;

$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' => $profils,
          array(
              'attr' => array(
                  'mapped'   => false,
                  '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()
;

现在一切都好,祝你好运。