在Doctrine选择语句中使用“case when” [英] Using a 'case when' in a Doctrine select statement

查看:133
本文介绍了在Doctrine选择语句中使用“case when”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

它在case上是barfs。文件没有提到可能(或不)。



我想知道Doctrine是否缺乏这样做的能力。如果是这样,这是一个相当重要的遗漏。有人知道一个解决方法吗?

解决方案

我在这里也有同样的问题。我的项目很老了,试图很快修复。所以我只是修改一些Doctrine的代码,所以我可以使用case when。这是我的Doctrine 1.1.3的代码。



Doctrine / Query.php,将行从658更改为674:



如果($($ 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。 '__'。 $别名;
}
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;

这不是一个很大的变化,但它帮助我...


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

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?

解决方案

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, 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”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆