如何在没有FROM子句的情况下测试sql代码?

时间:2010-09-23 12:05:47

标签: symfony1 doctrine

我想测试DQL是否支持这种语法:ROUND(NOW(), 'YEAR')(MySQL确实如此)。这是我在MySQL控制台中尝试的内容:

mysql> SELECT ROUND(NOW(), 'YEAR');
+----------------------+
| ROUND(NOW(), 'YEAR') |
+----------------------+
|       20100923135639 |
+----------------------+

在DQL中尝试相同的事情就是:

techosaure :( \>~/workspace/carel  ./symfony  doctrine:dql --show-sql "Select ROUND(NOW(),'YEAR')";
>> doctrine  executing dql query
DQL: Select ROUND(NOW(),'YEAR')
PHP Fatal error:  Maximum function nesting level of '100' reached, aborting! in /opt/symfony-1.4.6/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Query/Tokenizer.php on line 352

如何在不使用FROM子句的情况下在DQL中测试函数?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

Doctrine_Query::create()
  ->from('Model m')
  ->select('m.id, ROUND(NOW(), "YEAR") AS round_test')
  ->fetchOne();

我知道您不想使用FROM子句,但我认为DQL查询必须在模型上运行。

greg0ire>就我而言,这是我尝试过的(和结果):

techosaure :( \>~/workspace/carel  ./symfony  doctrine:dql --show-sql "Select ROUND(NOW(),'YEAR') FROM AdminEntity";
>> doctrine  executing dql query
DQL: Select ROUND(NOW(),'YEAR') FROM AdminEntity
SQL: SELECT ROUND(NOW(), 'YEAR') AS a__0 FROM admin_entity a
found 1 results
-
  ROUND: '20100923151158'