dsomm80482 2015-06-17 10:29
浏览 33
已采纳

通过路径参数过滤和学说查询获取并显示正确的数据

To begin I have this SQl request:

SELECT  pc.id, pc.nom_point_comptage, e.id, e.nom_ensemble, p.id, p.nom_parc
FROM points_comptage pc , ensembles e , parcs_immobilier p
WHERE pc.ensembles_id = e.id
AND e.parcs_immobilier_id = p.id

This query allows me to get the points comptage which are in the ensembles owned to a specific parc

So in my symfony controller I made a DQL with doctrine. This is the code for the controller:

/**
 *
 * @Route("/gestioncompteurs/pointscomptageByparcs", name="dataTablePointsComptageParc")
 * @Method("get")
 */
public function pointsComptageByParcAction($nomParc)
{
  $em=$this->getDoctrine()->getManager();

  $query = $em->createQuery('SELECT  p.nomParc, e.nomEnsemble, pc.invariantPointComptage /*and more like my SQL request...*/
                             FROM MySpaceMyBundle:ParcsImmobilier p, MySpaceMyBundle:Ensembles e, MySpaceMyBundle:PointsComptage pc
                               WHERE pc.ensembles = e.id
                               AND e.parcsImmobilier = p.id');

  $pointComptage = $query->getResult();

  return $this->render('MySpaceMyBundle:PointsComptage:dataTablePointsComptage.html.twig', array(
        'pointComptage' => $pointComptage));
}

But in my twig, first I need to filter by parc, so I create a select tag where there are all my parcs like this:

<select class="form-control input" id="filterByParc" name="filterByParc">
  <option value="" disabled selected>sélectionnez un parc</option>
    {% for parcs in parc %}
      <option value="{{ path('dataTablePointsComptageParc', {'nomParc': parcs.nomParc}) }}">{{ parcs.nomParc }}</option>
    {% endfor %}
</select>

As you can see, the value for the selected options in my select tag is a path(url) for a view. Because I need to return in javascript a datatables filtering by the parc I choose (route parameter).

All works well, that is to say, my controller and my javascript display correctly my datatable, but with no data.

On phpMyAdmin, my request works well, but not with Doctrine in my controller.

To explain exactly what I am trying to do and what I need:

first: I select a parc name in my select tag in my view; second: my choice returns me a datatable filtering by the name of my parc I choose, so the datas in my table have to be all the points comptage in my ensembles owned to the parc I choose first in the select tag.

Someone know where my problem occured?

  • 写回答

2条回答 默认 最新

  • douan7601 2015-06-17 12:47
    关注

    I finally found the logic of filtering with a DQL and a route parameter.

    See my controller code here:

    /**
     *
     * @Route("/gestioncompteurs/pointscomptageByparcs", name="dataTablePointsComptageParc")
     * @Method("get")
     */
    public function pointsComptageByParcAction($nomParc)
    {
      $em=$this->getDoctrine()->getManager();
    
      $query = $em->createQuery("SELECT  p.nomParc, e.nomEnsemble, pc.invariantPointComptage /*and more like my SQL request...*/
                                 FROM MySpaceMyBundle:ParcsImmobilier p, MySpaceMyBundle:Ensembles e, MySpaceMyBundle:PointsComptage pc
                                   WHERE pc.ensembles = e.id
                                   AND e.parcsImmobilier = p.id
                                   AND p.nomParc = '$nomParc'");
    
      $pointComptage = $query->getResult();
    
      return $this->render('MySpaceMyBundle:PointsComptage:dataTablePointsComptage.html.twig', array(
            'pointComptage' => $pointComptage, 'parc' => $parc));
    }
    

    I need to pass the $nomParc in my DQl to recover the right datas in order to match with the data I pass to the route parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路