Zend_Db_Select:重新组合where子句中的条件

时间:2010-04-29 11:39:57

标签: php sql zend-framework select zend-db-table

我想做这样的事情:

$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'");

但由于orWhere子句未在唯一的AND子句中组合在一起,因此输出的查询不正确。我想知道是否有可能在一对父母中重新组合那些不喜欢的条款。

我目前的替代方案如下:

  //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)");
  }

谢谢!

3 个答案:

答案 0 :(得分: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)");
}

答案 1 :(得分:0)

Zend_Db_Select支持您正在寻找的orWhere子句。

答案 2 :(得分:0)

您当前的选择似乎是最佳选择。这是一个略有改动的版本,它使用正确的引用,万一有什么不好的东西滑入$ bot

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