通过路由参数过滤和条令查询获取并显示正确的数据

get and display the right datas with route parameters filtering and a doctrine query

首先我有这个 SQl 请求:

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

此查询允许我获取属于特定 parc

ensembles 中的 points comptage

所以在我的 symfony 控制器中我制作了一个 DQL with doctrine。这是控制器的代码:

/**
 *
 * @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));
}

但在我的树枝中,首先我需要按 parc 进行过滤,因此我创建了一个 select 标签,其中包含我所有的 parc,如下所示:

<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>

如您所见,我的 select 标记中 selected 选项的值是视图的路径 (url)。因为我需要 return 在 javascript 中按我选择的 parc(路由参数)过滤数据表。

一切正常,也就是说,我的控制器和 javascript 正确显示了我的数据表,但没有数据。

phpMyAdmin 上,我的请求运行良好,但在我的控制器中 not Doctrine .

准确解释我想做什么和我需要什么:

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.

有人知道我的问题出在哪里吗?

我认为这很有道理。

Doctrine 通过 DQL 对实体进行操作(不是 SQL)。至少你可以尝试用 getArrayResult() 代替 getResult().

事实上,你应该使用 DBAL Connection:

$em = .... // Your EntityManager
$sql = " .... ";
$pointComptage = $em->getConnection()->fetchAssoc($sql);

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

希望这对您有所帮助...

我终于找到了用一个DQL和一个路由参数过滤的逻辑

在此处查看我的控制器代码:

/**
 *
 * @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));
}

我需要在我的 DQl 中传递 $nomParc 来恢复正确的数据,以便与我传递给路由参数的数据相匹配。