MySQL User defined variable within Doctrine and Symfony(Doctrine 和 Symfony 中的 MySQL 用户定义变量)
问题描述
我有以下 Doctrine 声明,效果很好.
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);
我需要在其中添加行数.现在我知道使用原始 SQL 可以做到这一点;
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
所以我的问题是,我怎样才能让它与 Symfony 1.4 和 Doctrine 一起运行?我在这个项目中使用 MySQL.
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.
推荐答案
如果你正在使用 MySQL,并且没有预见到切换到其他 DBMS,也许你可以尝试
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);
这篇关于Doctrine 和 Symfony 中的 MySQL 用户定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine 和 Symfony 中的 MySQL 用户定义变量


基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01