多个条件代码点火器活动记录


Multiple where condition codeigniter active record

我想用ANDOR进行多个查询。假设我的数组是:

array (size=8) 
      0 => array (size=2) 
              'timeline.type' => string 'videos' (length=6)                
              'timeline.sourceId' => string '7' (length=1) 
      1 => array (size=2) 
              'timeline.type' => string 'loadshedding' (length=12) 
              'timeline.sourceId' => string '5' (length=1) 
      2 => array (size=2) 
              'timeline.type' => string 'news' (length=4) 
              'timeline.sourceId' => string '3' (length=1)
      3 => array (size=2) 
              'timeline.type' => string 'news' (length=4) 
              'timeline.sourceId' => string '5' (length=1) 

上面的数组可能是动态的。我想进行活动记录查询,例如:

 (timeline.type = 'videos' AND timeline.sourceId = 7) OR (timeline.type = 'loadshedding' AND timeline.sourceId = 5) OR (timeline.type = 'news' AND timeline.sourceId = 3) // and so on.

我试过了:

 $this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
 $this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
 $this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
 foreach($sources as $source){   //$sources is an array like given above
        $this->db->or_where($source);
 }
 $this->db->order_by("timeline.id", "DESC");
 $this->db->limit(15);

但是当我回声$this->db->last_query();查看查询时。它只返回SELECT * FROM timeline。可能是什么问题。谢谢。

你的代码应该是这样的

 $sources = array (
                    0 => array ( 
                          'timeline.type' =>  'videos',             
                          'timeline.sourceId' =>  '7' ), 
                    1 => array ( 
                          'timeline.type' =>  'loadshedding',
                          'timeline.sourceId' =>  '5' ) ,
                    2 => array (
                          'timeline.type' =>  'news', 
                          'timeline.sourceId' =>  '3' ),
                    3 => array ( 
                          'timeline.type' =>  'news',
                          'timeline.sourceId' =>  '5') 
                );
end($sources);         // move the internal pointer to the end of the array
$lastkey = key($sources);  
$pr = "";
foreach($sources as $key=>$source)
{   
    if($key != $lastkey)
    {
        $pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"]  . ") OR ";
    }
    else
    {
        $pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"]  . ")";
    }   
}
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
$this->db->where($pr);
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);