2009-10-14 62 views
4

我有一個選擇查詢我想和原則執行:使用「的情況下,當」一個主義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(); 

它barfs的「案例」。文件沒有提到它是可能的(或不)。

我想知道教義是否缺乏這樣做的能力。如果是這樣,這是一個相當大的疏忽。有誰知道解決方法?

+1

請一個PHP代碼添加到這個問題 – tzup 2009-10-14 08:48:15

+0

DONE ...至少15個字符: -/ – dland 2009-10-16 21:45:50

回答

1

我這裏有同樣的問題。我的項目很舊,並試圖快速修復它。所以我只是修改一些Doctrine的代碼,這樣我就可以使用「case when」。這是我爲學說1.1.3的代碼。

主義/ 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; 

這不是一個很大的變化,但它幫助我......

-3

我建議你不要使用這種CASE語法來解決這個問題。它看起來很棘手。

爲什麼你不希望通過$結果集

$resultset = Doctrine_Query::create() 
    ->select("t.code, t.description, t.id_outcome") 
    ->from('LuOutcome t') 
    ->orderBy('t.rank') 
    ->fetchArray(); 

,然後循環,創造這一領域(IN_PROGRESS)手動視(id_outcome)值。你可以使用一些小的簡單的小方法。

優點:

  • 簡單
  • 可讀
+13

壞主意。循環訪問代碼中的結果集來執行修正是一個跡象,表明您沒有要求數據庫做足夠的工作。我發現這是PHP開發人員的共同特徵。 這個問題看起來有點棘手:除了SQL語句中的case語句是非常有用的,可惜的是Doctrine不承認它。 – dland 2009-10-21 12:57:10

23

我有同樣的問題,並在乍看之下,似乎有一種變通方法。我相信你可以'僞裝'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(); 
+2

這必須被接受爲答案+1; – onalbi 2015-10-08 10:48:22

+0

它也在WHERE子句中工作。 – pwagner 2017-02-21 14:27:35