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

Php Symfony:如何使用另一个实体的选择字段创建表单?,php,symfony,Php,Symfony,我有一个表单,它必须包含一个选项字段,该字段显示来自另一个实体(而不是表单中使用的实体)的内容,因此我尝试创建它: $user = new user(); $profils=$rep->findAll(); $form2 = $this->createFormBuilder($user,array('csrf_protection' => false)) ->add('id', 'text',array('attr'

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

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

PaaaaaSe帮助我,因为我是新的与SimfOy,我可能陷入了你可能认为愚蠢的问题,对不起,但我自己找不到解决办法。< / P > P的一种方式是这样做:

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

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

现在就这些,祝你好运。

我通过更改'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();

您使用的是哪个版本的symfony2?我使用symfony 2.6thaaaanx为您提供帮助:但错误表明,表单的基本实体“用户”中缺少实体“profil”的某些方法;所以我需要克服这个实体,好像我想以实体“user”中不存在的形式添加一个额外的字段如果它是一个具有简单类型的字段,我可以user“mapped”=>false这就是错误:属性profils和方法getProfils、profils、isProfils、hasProfils,_uu在类CNAM\CMSBundle\Entity\user中存在并具有公共访问权限。检查实体之间的关系,不要忘记在我知道的实体上指定存储库。我的问题是:如何使此字段显示从另一个实体获取的内容,我的意思是“profils”包含数据库表的内容,我想通过它在formloop的选择字段中显示它,好吗?