Mysql functions for Doctrine Query Builder

im trying to create one of my old mysql queries in Doctrine Query Builder but im returning error Error: expected known function received by 'SEC_TO_TIME'

So, I assume that the doctrine does not like the mysql function SEC_TO_TIME , but it looks like it is like AVG , COUNT , etc. Is there any other form that uses the Doctrine_RawSQL class to create a query query to execute a query?

thanks

+7
source share
2 answers

Those AVG and Count are aggregate functions of DQL, they have nothing to do with SQL. So there is no way to invoke UDF from DQL, except for RawSQL

But if you use doctrine2, you can take a look at Adding Your Own Functions in DQL and Extending DQL in Doctrine 2: Custom Functions .

+2
source

I know this question is very old, but I answer this question for those who have the same error.
you just need to add the code below as a file called "SecToTime.php", as shown below.

YourProjectName \ Library \ Doctrine \ DoctrineExtensions \ Query \ MySql \ SecToTime.php

And put the code below into the above file named "SecToTime.php".

 <?php namespace DoctrineExtensions\Query\Mysql; use Doctrine\ORM\Query\AST\Functions\FunctionNode, Doctrine\ORM\Query\Lexer; class SecToTime extends FunctionNode { public $time; /** * @override */ public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) { return 'SEC_TO_TIME(' . $sqlWalker->walkArithmeticPrimary($this->time) . ')'; } /** * @override */ public function parse(\Doctrine\ORM\Query\Parser $parser) { $parser->match(Lexer::T_IDENTIFIER); $parser->match(Lexer::T_OPEN_PARENTHESIS); $this->time = $parser->ArithmeticPrimary(); $parser->match(Lexer::T_CLOSE_PARENTHESIS); } } 

you need to add the above file name to your doctrine.php file for future reference. I hope you know how to add this.

If you have any questions, feel free to ask.

+1
source

Source: https://habr.com/ru/post/888424/


All Articles