doumang20060820 2015-09-24 07:59
浏览 21
已采纳

Symfony形式和循环与学说[关闭]

I have recently started using Symfony. I write a doctrine query and i found the values I needed but i have the problem when i want to enter values in a form's textbox with a for loop.. This is my query:

 $id_user = $user->getId();
        $query = $em->createQueryBuilder()
     ->select('ur','uu')   
     ->from('DtEcBundle:UserReferences', 'ur')
     ->innerJoin("ur.id_user","uu")
     ->where("ur.id_user = :id_user")
     ->setParameter("id_user",$id_user)
            ->getQuery();
            $userpyramid = $query->getResult();

And now i would insert my values in a form..

 $form = $this->get('form.factory')->createNamedBuilder('form', 'form')
        ->setMethod('POST')
        ->setAction($this->generateUrl('profilo_secondlevel'))
        ->add('idreferenced', 'text', array(
                'data' =>  '',
            ))
        ->add('save', 'submit', ['label' => 'Prova'])
        ->getForm();
  • 写回答

1条回答 默认 最新

  • dongzhuo5425 2015-09-24 10:59
    关注

    If I understood your question correctly, you don't really need any form!
    Just pass the ArrayCollection to twig, iterate over it and display all values you need. After that, define a route paramized by id that you can access by a link.

    So, basically

    //somewhere in controller
    /**
     * @Route("/showSomethingByUserId/{uid}") <-- this could be also inserted into .yml file
     */
    public function showSomethingByUserIdAction($uid)
    {
        //implement some code to check $uid (just an example)
    }
    

    Pass $userpyramid to twig

    //somewhere in controller
    public function extractAllIdUsersAction()
    {
        [...]
        //should be better to create a repository for this
        $id_user = $user->getId();
        $query = $em->createQueryBuilder()
            ->select('ur','uu')   
            ->from('DtEcBundle:UserReferences', 'ur')
            ->innerJoin("ur.id_user","uu")
            ->where("ur.id_user = :id_user")
            ->setParameter("id_user",$id_user)
            ->getQuery();
        $userpyramid = $query->getResult();
    
        return $this->render('YourBundle:Folder:TwigName.html.twig, array(
            'userPyramid' => $userpyramid)
        );
    }
    

    Then in your twig

    {% for user in userpyramid %}
        {{ user.name }}
        {{ user.surname }}
        <a href="{{ path('showSomethingByUserId', {'uid': user.id}) }}">Access to user infos</a>
    {% endfor %}
    

    Of course your names may vary accordingly

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)