doujiu9172 2015-09-23 10:18
浏览 24

如何在symfony2中下载数据时在表单构建器中应用条件?

I'm new to symfony I have a web controller , In Web controller i get all user's name from UserRepository where roll is not Admin. every thing fine with createAction but when i try to update web data in editAction i get an error like

The value of type "object" cannot be converted to a valid array key.

In createAction im getting users

   $all_users = $em->getRepository('MWANMOBILEBIBundle:User')->allWebUser();

      // var_dump($all_users);

        $user_arr=array();
        foreach ($all_users as $single_user){
            $user_arr[$single_user['id']]=$single_user['username'];

        }

$form = $this->createFormBuilder()
        ->add('description', 'text')
        ->add('primaryUrl', 'text')
        ->add('testUrl', 'text')
        ->add('notes', 'textarea')
        ->add('save', 'submit', array('label' => 'Create Web Service'))

        ->add('user', 'choice', array('choices' => $user_arr ))
        ->add('parent', 'choice', array('choices' =>     $user_service,'required' => false,))
        ->add('serverStatus', 'checkbox')
        ->add('statusEmail' ,'email')
        ->add('statusMessage' , 'textarea')
        ->getForm();

In createFormBuilder below mentioned line is working fine

       ->add('user', 'choice', array('choices' => $user_arr )) 

But when I'm using in edit action i got an error

The value of type "object" cannot be converted to a valid array key.

here is my editAction code

        $em = $this->getDoctrine()->getManager();
        $web = $em->getRepository('MWANMOBILEBIBundle:Web')->find($id);

       $all_users = $em->getRepository('MWANMOBILEBIBundle:User')->allWebUser();

         var_dump($all_users);

        $user_arr=array();
        foreach ($all_users as $single_user)
        {
            $user_arr[$single_user['id']]=$single_user['username'];
        }



        $form = $this->createFormBuilder($web)

        ->add('description', 'text')
        ->add('primaryUrl', 'text')
        ->add('testUrl', 'text')
        ->add('notes', 'textarea')
        ->add('user', 'choice', array('choices' => $user_arr ))
        //->add('user', 'entity', array('class' => 'MWANMOBILEBIBundle:User',
        //      'property' => 'username',))
        ->add('parent', 'entity', array('class' => 'MWANMOBILEBIBundle:Service', 
              'property' => 'description' ))
        ->add('serverStatus', 'checkbox')
        ->add('statusEmail' ,'email')
        ->add('statusMessage' , 'textarea')
        ->add('save', 'submit', array('label' => 'Update Web Service'))
        ->getForm();
  • 写回答

1条回答 默认 最新

  • douchun1961 2015-09-23 15:06
    关注

    Check out the Entity Form field you can perform your queries directly in that without your foreach, and in your Entity, set a __tostring() method to return which value to use when saving in your db

    Welcome to Symfony mate :)

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计