Php 订单日期查询,语法错误
我想使用下面的查询对门店下订单的日期进行排序。我希望按升序执行这些操作,但是当我尝试测试运行此操作时,会出现以下错误: [语法错误]第0行第7列:错误:应为IdentificationVariable| ScalarExpression |聚合Expression |函数声明| PartialObjectExpression |“(“子选择”)“| CaseExpression,获取 “订单” 我在.twig模板中呈现orderResultPhp 订单日期查询,语法错误,php,symfony,doctrine-orm,query-builder,dql,Php,Symfony,Doctrine Orm,Query Builder,Dql,我想使用下面的查询对门店下订单的日期进行排序。我希望按升序执行这些操作,但是当我尝试测试运行此操作时,会出现以下错误: [语法错误]第0行第7列:错误:应为IdentificationVariable| ScalarExpression |聚合Expression |函数声明| PartialObjectExpression |“(“子选择”)“| CaseExpression,获取 “订单” 我在.twig模板中呈现orderResult return $this->render('ad
return $this->render('admin/store/sales/adminsales.html.twig', array(
'orderResult' =>$orderResult
));
方法
createQueryBuilder
不接受参数,因此请将别名放入select
方法中,如下所示:
$orderSales = $this->getDoctrine()->getRepository("AppBundle:Order")->createQueryBuilder();
$orderSales->select('order');
$orderSales->orderBy('order.date', 'asc');
$orderSales = $orderSales->getQuery();
$orderResult = $orderSales->getResult();
希望此帮助您应该在存储库中声明QueryBuilder,例如(createQueryBuilder接受参数): 在控制器中:
$orderResult = $this->getDoctrine()->getRepository("AppBundle:Order")->findOrdersByDate();
编辑:正如Imanali Mamadiev所指出的:在
createQueryBuilder('order')中使用wordorder
时,将“order”替换为“o”,以避免SQL关键字冲突
更改订单
至
只需o
例如createQueryBuilder('o')
结果:
$orderSales = $this->getDoctrine()->getRepository("AppBundle:Order")->createQueryBuilder('o');
$orderSales->orderBy('o.date', 'asc');
$orderSales = $orderSales->getQuery();
$orderResult = $orderSales->getResult();
作为建议,您应该创建一个独立的存储库方法,而不是在控制器内创建queryBuilder。谢谢,我将对此进行更改
$orderResult = $this->getDoctrine()->getRepository("AppBundle:Order")->findOrdersByDate();
$orderSales = $this->getDoctrine()->getRepository("AppBundle:Order")->createQueryBuilder('o');
$orderSales->orderBy('o.date', 'asc');
$orderSales = $orderSales->getQuery();
$orderResult = $orderSales->getResult();