1

I want to execute date() function in Symfony2 query, below is what I've have tried but it gives me an error.

$query=$em->createQueryBuilder()
           ->select('date(a.created_at), COUNT(date( a.created_at) ) AS        count_user')
           ->from('ContrateAdminBundle:Artist', 'a')
           ->where("date(a.created_at) BETWEEN '{$fromdateCelebrities}' AND '{$todateCelebrities}'")
           ->groupBy('date(a.created_at)')
           ->getQuery();

I have also pasted the output string of my query, which I can execute without any error directly in MySQL.

select date(created_at) created_at, count(*) from Artist where date(created_at) BETWEEN '2015-01-05' AND '2015-02-25' group by date(created_at)
3
  • Could you just use the sql function DATE()? Commented Mar 3, 2015 at 9:18
  • how can i used sql function DATE()? Commented Mar 3, 2015 at 9:20
  • possible duplicate of Select entries between dates in doctrine 2 Commented Mar 3, 2015 at 9:25

1 Answer 1

4

The error is caused by doctrine 2, it does not understand the mysql method date, you should create it manualy.

I saw the sollution alredy on Create custom Doctrine 2 Method

  • Create a php class named DateFunction.php with the following code:

    use Doctrine\ORM\Query\AST\Functions\FunctionNode;
    use Doctrine\ORM\Query\Lexer;
    use Doctrine\ORM\Query\SqlWalker;
    use Doctrine\ORM\Query\Parser;
    
    class DateFunction extends FunctionNode
    {
        private $arg;
    
        public function getSql(SqlWalker $sqlWalker)
        {
            return sprintf('DATE(%s)', $this->arg->dispatch($sqlWalker));
        }
    
        public function parse(Parser $parser)
        {
            $parser->match(Lexer::T_IDENTIFIER);
            $parser->match(Lexer::T_OPEN_PARENTHESIS);
    
            $this->arg = $parser->ArithmeticPrimary();
    
            $parser->match(Lexer::T_CLOSE_PARENTHESIS);
        }
    }
    
  • In your controller or repository after you got the $em register the new method

    $em->getConfiguration()->addCustomDatetimeFunction('DATE', 'DateFunction');

This will register your custom doctrine method;

Sign up to request clarification or add additional context in comments.

6 Comments

can you give me the solution because i am totally new on symfony2
i am going to try this if still i have faced any problem then i`ll let you know
where can i create this class?
I'l sugest to make a folder in your bundle DoctrineCustomFunctions or DoctrineFunctions and place it there, or you can use the Symfony 2 sugested way, create the DQL folder and place them there, you can then make them global by configuring them in config.yml follow the link symfony.com/doc/current/cookbook/doctrine/…
This is the way Doctrine 2 works, can't help you more
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.