Using a #39;case when#39; in a Doctrine select statement(在 Doctrine 选择语句中使用“case when)
问题描述
我想用 Doctrine 执行一个选择查询:
I have a select query I'd like to perform with Doctrine:
$resultset = Doctrine_Query::create()
->select("t.code, t.description, case when t.id_outcome = 1 then 1 else 0 end as in_progress")
->from('LuOutcome t')
->orderBy('t.rank')
->fetchArray();
它对案例"产生了影响.文档没有提到它可能(或不可能).
And it barfs on the 'case'. The documentation does not mention that it's possible (or not).
我想知道 Doctrine 是否缺乏这样做的能力.如果是这样,这是一个相当大的遗漏.有人知道解决方法吗?
I'm wondering if Doctrine lacks the capacity to do so. If so, it's a rather major omission. Does anyone know of a work-around?
推荐答案
我在这里遇到了同样的问题.我的项目很旧,并试图快速修复它.所以我只是稍微修改一下 Doctrine 的代码,这样我就可以使用case when"了.这是我的 Doctrine 1.1.3 代码.
I had the same problem here. My project is very old, and tried to fix it quickly. So I just change a little bit the code for Doctrine so I can use "case when". This is my code for Doctrine 1.1.3.
Doctrine/Query.php,将658行改为674行:
Doctrine/Query.php, change lines from 658 to 674:
if (count($terms) > 1 || $pos !== false) {
if($terms[0]=='case')
{
$terms=explode(" as ", $reference);
$expression = array_shift($terms);
$alias = array_pop($terms);
if ( ! $alias) {
$alias = substr($expression, 0, $pos);
}
$componentAlias = $this->getExpressionOwner($expression);
$tableAlias = $this->getTableAlias($componentAlias);
$expression=str_replace($componentAlias, $tableAlias, $expression);
$index=0;
$sqlAlias = $tableAlias . '__' . $alias;
}
else
{
$expression = array_shift($terms);
$alias = array_pop($terms);
if ( ! $alias) {
$alias = substr($expression, 0, $pos);
}
$componentAlias = $this->getExpressionOwner($expression);
$expression = $this->parseClause($expression);
$tableAlias = $this->getTableAlias($componentAlias);
$index = count($this->_aggregateAliasMap);
$sqlAlias = $this->_conn->quoteIdentifier($tableAlias . '__' . $index);
}
$this->_sqlParts['select'][] = $expression . ' AS ' . $sqlAlias;
这不是一个很大的变化,但它帮助了我......
It's not a great change, but it helped me...
这篇关于在 Doctrine 选择语句中使用“case when"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Doctrine 选择语句中使用“case when"
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01