在Doctrine select语句中使用'case when'

dla*_*and 6 php sql doctrine

我有一个我想用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();
Run Code Online (Sandbox Code Playgroud)

它就是'案件'上的barfs.文档没有提到它是否可能(或没有).

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

Mik*_*e B 25

我刚遇到同样的问题,乍一看似乎有一个解决方法.我相信您可以"欺骗"Doctrine Select解析器,将其作为子查询处理,方法是将其包装在括号中.

尝试一下:

$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();
Run Code Online (Sandbox Code Playgroud)


小智 0

我在这里也遇到了同样的问题。我的项目很旧,并试图快速修复它。所以我只是稍微改变了 Doctrine 的代码,这样我就可以使用“case when”。这是我的 Doctrine 1.1.3 代码。

Doctrine/Query.php,将第 658 行更改为 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;
Run Code Online (Sandbox Code Playgroud)

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