2010-04-29 102 views
2

我願做這樣的事情重組的條件:Zend_Db_Select對象:在where子句

$select = $myTbl->select() 
->from('download_log') 
->joinLeft(...... etc........ 
->joinLeft(...... etc........ 
->joinLeft(...... etc........); 

//Filter all configured bots (Google, Yahoo, etc.) 
if(isset($this->_config->statistics->bots)){ 
$bots = explode(',',$this->_config->statistics->bots); 
foreach ($bots as $bot){ 
    $select = $select->where("user_agent NOT LIKE '%$bot%'"); 
} 
} 

$select = $select->where("download_log.download_log_ts BETWEEN '".$start_date." 00:00:00' AND '".$end_date." 23:59:59'"); 

但outputed查詢是因爲orWhere條款的不正確的獨特的條款沒有組合在一起。我想知道是否有可能在一對父級中重新組合那些NOT LIKE子句。

我現在的選擇是如下:

//Filter all configured bots (Google, Yahoo, etc.) 
    if(isset($this->_config->statistics->bots)){ 
    $bots = explode(',',$this->_config->statistics->bots); 
    foreach ($bots as $bot){ 
    $stmt .= "user_agent NOT LIKE '%$bot%' AND "; 
    } 
    $stmt = substr($stmt,0,strlen($stmt)-4); //remove the last OR 
    $select = $select->where("($stmt)"); 
    } 

謝謝!

回答

0

您目前的選擇看起來是最好的選擇。這裏有一個稍微改動的版本,使用適當的引用,以防萬一有什麼不好的滑入$機器人

$botWhere = ''; 
foreach ($bots as $bot){ 
    $botWhere .= $myTbl->getAdapter()->quoteInto('user_agent NOT LIKE ? AND ', "%$bot%"); 
} 
$botWhere = trim($botWhere, ' AND '); 
$select = $select->where($botWhere); 
0

Zend_Db_Select支持orWhere條款,這是你在找什麼。

1

謝謝!我終於這樣做了:

//Filter all configured bots (Google, Yahoo, etc.) 
if(isset($this->_config->statistics->bots)){ 
     $bots = explode(',',$this->_config->statistics->bots); 
     foreach ($bots as $bot){ 
       $stmt .= $myTbl->getAdapter()->quoteInto("user_agent NOT LIKE ? AND ",%$bot%); 
     } 
     $stmt = trim($stmt, ' AND '); //remove the last AND 
     $stmt .= 'OR user_agent IS NULL'; 
     $select = $select->where("($stmt)"); 
}