2011-07-13 4 views
0
public static function getAuctions($other = false) 
{ 
    $q = Doctrine::getTable('auctions') 
     ->createQuery('u') 
     ->select('TIMEDIFF(ends_at, NOW()) as time, ur.username as username, u.last_bider as last_bider, 
     pr.img_path as img_path, u.curr_price as price, pr.title as title, u.ends_at as ends_at, 
     ur.bids_left as bids_left') 
     ->leftJoin('u.Products pr') 
     ->leftJoin('u.sfGuardUser ur') 
     ->where('u.ends_at > ?', date('Y-m-d H:i:s', time() - 5)) 
     ->andWhere('u.status = ?', 'Live'); 


    ***if($other == true) 
     $q->andWhere('time > ?', '01:00:00');*** 

    $q->orderBy('time'); 

    return $q->execute(); 
} 

このクエリは、mysqlがtimeカラムを見つけることができないというif節で失敗します。私はそれを最初に「時間」と名づけました。Doctrineクエリは短縮形を認識しません:構築条件ベースのクエリ

私は

if($other == true) 
      $q->andWhere('TIMEDIFF(ends_at, NOW()) > ?', '01:00:00'); 

を使用している場合、それは動作しますが、私は自分自身を繰り返したくはありません。

これを修正する方法はありますか(またはdoctrineで条件ベースのクエリを作成するより実際的な方法がありますか?)。

+1

ただし、 'TIMEDIFF(ends_at、NOW())> '01:00:00 ''は' ends_at'のインデックスを使用できませんが、テーブル全体をスキャンする必要がありますまたは少なくとも他の条件で選択されたすべての行)。代わりに 'WHERE ends_at

答えて

2

where句の後にselect句が実行されるため、where句のselect句で定義された別名を参照することはできません。何ができるか

は副選択でテーブルを置換することである:それはあなたのケースでは価値がある場合

select time, id 
from (
    select TIMEDIFF(ends_at, NOW()) as time, id 
    from auctions 
) auctions 

は知ってはいけません。

関連する問題