Php Doctrine和Symfony中的MySQL用户定义变量
我有下面的条令声明,效果很好Php Doctrine和Symfony中的MySQL用户定义变量,php,mysql,symfony1,doctrine,symfony-1.4,Php,Mysql,Symfony1,Doctrine,Symfony 1.4,我有下面的条令声明,效果很好 $query = $this->createQuery('r') ->select('u.id, CONCAT(u.first_name, " ", LEFT(u.last_name,1)) as full_name, u.first_name, u.last_name, u.gender, r.run_time') ->innerJoin('r.ChallengeUser u')
$query = $this->createQuery('r')
->select('u.id, CONCAT(u.first_name, " ", LEFT(u.last_name,1)) as full_name, u.first_name, u.last_name, u.gender, r.run_time')
->innerJoin('r.ChallengeUser u')
->orderBy('run_time')
->execute(array(), Doctrine::HYDRATE_ARRAY_SHALLOW);
我需要在其中添加行计数。现在我知道使用原始SQL可以做到这一点
SET @rank=0;
SELECT @rank:=@rank+1 as rank, u.id, u.first_name ....etc
所以我的问题是,我如何让Symfony 1.4和Doctrine运行这个程序?
我正在为这个项目使用MySQL
编辑。。。 我想出来了
Doctrine_Manager::getInstance()->getCurrentConnection()->standaloneQuery('SET @rank=0;')->execute();
$query = $this->createQuery('r')
->select('r.run_time, @rank:=@rank+1 rank, r.user_id, CONCAT(u.first_name, " ", LEFT(u.last_name,1)) as full_name, u.first_name, u.last_name, u.gender')
->leftJoin('r.ChallengeUser u')
->orderBy('run_time')
->execute(array(), Doctrine::HYDRATE_ARRAY_SHALLOW);
在中添加独立查询以设置变量,并将内部联接交换为左联接。如果您使用的是MySQL,并且预计不会切换到其他DBMS,也许您可以尝试
$query = $this->createQuery('r')
->select('COUNT(u.id) as rank, u.id, CONCAT(u.first_name, " ", LEFT(u.last_name,1)) as full_name, u.first_name, u.last_name, u.gender, r.run_time')
->innerJoin('r.ChallengeUser u')
->orderBy('run_time')
->execute(array(), Doctrine::HYDRATE_ARRAY);
因为它需要作为一个json数组输出给flash,这样可以更好地操作它!好的,我会试着给你一个答案:)对不起,我试过了,找不到解决办法。我甚至尝试了Doctrine_RawSql,但它不允许在select中使用变量。