MySQL User defined variable within Doctrine and Symfony

懵懂的女人 提交于 2019-11-28 01:23:28

问题


I have the following Doctrine statement which works fine.

$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);

I need to add a row count into this. Now I know with raw SQL you can do this;

SET @rank=0;
SELECT @rank:=@rank+1 as rank, u.id, u.first_name ....etc

So my question is, how can I get this to run with Symfony 1.4 and Doctrine? I am using MySQL for this project.


Edit... I figured it out..

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);

Add the standalone query in to set the variable, and swap the inner join to a left join.


回答1:


if you are using MySQL, and no switches to other DBMS are foreseen, maybe you could try

$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);


来源:https://stackoverflow.com/questions/6541999/mysql-user-defined-variable-within-doctrine-and-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!