首先,我有一个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这个查询允许我获取points comptage,它位于属于特定parc的ensembles中。
所以在我的symfony控制器里,我用理论做了一个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');
$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>如您所见,选择标记中所选选项的值是视图的路径(Url)。因为我需要在javascript中返回由我选择的parc (路由参数)过滤的数据。
所有操作都很好,也就是说,我的控制器和javascript正确地显示了我的datatable,但是没有数据。
在phpMyAdmin上,我的请求运行得很好,但是在控制器中有Doctrine的没有。
为了准确地解释我想做什么和我需要什么:
first :我在视图中的select标记中选择了一个parc名称;第二个:我的选择根据我选择的parc的名称返回一个datatable过滤,所以我的表中的数据必须是
ensembles中的所有points comptage,而在select标记中首先选择的是parc。
有人知道我的问题出在哪里吗?
发布于 2015-06-17 12:47:01
最后,我找到了使用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));
}我需要在我的$nomParc中传递DQl以恢复正确的数据,以便与传递给路由参数的数据匹配。
发布于 2015-06-17 11:07:25
我觉得这很有道理。
Doctrine通过DQL (不是SQL)对实体进行操作。至少,您可以尝试使用getArrayResult()而不是getResult()。
实际上,您可能应该使用DBAL连接
$em = .... // Your EntityManager
$sql = " .... ";
$pointComptage = $em->getConnection()->fetchAssoc($sql);
return $this->render('MySpaceMyBundle:PointsComptage:dataTablePointsComptage.html.twig', array(
'pointComptage' => $pointComptage));希望这能帮上忙。
https://stackoverflow.com/questions/30888930
复制相似问题