Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order date query, syntax error

The following query I want to use to order the dates of order placed by stores. I want to do them in ascending order however when I try to test run this I get the following error:

[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();

I do render orderResult in the .twig template

return $this->render('admin/store/sales/adminsales.html.twig', array(
                            'orderResult' =>$orderResult
    ));
like image 578
KevinTheGreat Avatar asked Mar 12 '23 17:03

KevinTheGreat


2 Answers

You should declare your QueryBuilder in your Repository such as (createQueryBuilder accepts argument) :

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

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

In your controller :

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

EDIT : As Imanali Mamadiev has pointed out : replace "order" by just "o" to avoid SQL Keywords conflicts

like image 119
Delphine Avatar answered Mar 15 '23 06:03

Delphine


Error when using word order in createQueryBuilder('order')

Change order

To

just o for example createQueryBuilder('o')

Result:

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

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

like image 44
Imanali Mamadiev Avatar answered Mar 15 '23 06:03

Imanali Mamadiev