SQL future dates in request

74 Views Asked by At

I'm trying to execute an SQL request to get all future events.

So far, it looks like this :

$dql   = "SELECT a FROM AOFVHFlyBundle:Flight a WHERE precisedate >= NOW()";
$query = $em->createQuery($dql);

But I get the following error:

line 0, col 59: Error: Expected known function, got 'NOW'

How can i get all my future flights ?

3

There are 3 best solutions below

0
On BEST ANSWER

The DQL Function you are looking for is the CURRENT_TIMESTAMP(). As described in the doc.

0
On

Try using CURRENT_TIMESTAMP no parenthesis.

0
On

If you're interested, you can create your own DQL function. Just an another option.

For full example: How to create and use custom built doctrine DQL function in symfony

# sport/src/Football/FrontendBundle/DQL/NowTs.php
namespace Football\FrontendBundle\DQL;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;

class NowTs extends FunctionNode
{
    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(SqlWalker $sqlWalker)
    {
        return 'NOW()';
    }
}

In your query:

->where('..... < NOW() ....')