7

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

そして、それは「ケース」にbarfsします。ドキュメントには、それが可能かどうかについては言及されていません。

Doctrine にはそうする能力がないのではないかと思っています。もしそうなら、それはかなり大きな省略です。回避策を知っている人はいますか?

4

5 に答える 5

28

私はちょうど同じ問題を抱えていましたが、一見したところ、回避策があるようです。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();
于 2010-10-23T22:25:19.207 に答える
4

ケースステートメントは、ある時点でドクトリンに追加されたようです: https://github.com/doctrine/orm-documentation/commit/189c729f15d2fafecf92662cad9553c2ec3dccd7#diff-0

于 2012-02-01T01:55:54.527 に答える
3

Doctrine Query LanguageのBNF 文法には、構造に関連するものは何も含まれていないようですCASE

于 2009-10-14T09:15:25.270 に答える
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;

大きな変化ではありませんが、私を助けてくれました...

于 2012-03-07T03:09:13.673 に答える