php - 订单日期查询,语法错误

标签 php symfony doctrine-orm query-builder dql

我想使用以下查询来订购商店下订单的日期。我想按升序执行它们,但是当我尝试测试运行时出现以下错误:

[Syntax Error] line 0, col 7: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got 'order'

$orderSales = $this->getDoctrine()->getRepository("AppBundle:Order")->createQueryBuilder('order');

    $orderSales->orderBy('order.date', 'asc');
    $orderSales = $orderSales->getQuery();
    $orderResult = $orderSales->getResult();

我在 .twig 模板中渲染 orderResult

return $this->render('admin/store/sales/adminsales.html.twig', array(
                            'orderResult' =>$orderResult
    ));

最佳答案

您应该在您的存储库中声明您的 QueryBuilder,例如 (createQueryBuilder 接受参数):

public function findOrdersByDate()
{
    $qb = $this->createQueryBuilder('o')
        ->orderBy('o.date', 'asc');

    return $qb
        ->getQuery()
        ->getResult();
}

在你的 Controller 中:

$orderResult = $this->getDoctrine()->getRepository("AppBundle:Order")->findOrdersByDate();

编辑:正如 Imanali Mamadiev 所指出的:将“order”替换为“o”以避免 SQL 关键字冲突

关于php - 订单日期查询,语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398915/

相关文章:

php - 在哪里为 Woocommerce 添加一个钩子(Hook)函数代码片段?

php - 通过PHP访问远程数据库MySQL

php - 代理对象而不是 Doctrine 中的实体

symfony - Doctrine 2 : disable lazy loading/proxy generation.

php - 语义错误 - 找不到常量 X,类...错误

php - 通过 MySQL 查询插入逗号分隔值

php-login 项目(最小)数据库连接问题

php - 从 Symfony 命令调用服务失败并出现 "The container cannot be retrieved as the application instance is not yet set."错误,为什么?

symfony - Symfony 和 Doctrine 中的批量插入 : How to select batch size?

Symfony 6 - 本地主机的连接被拒绝