I have a call to a model which is just a standard class (not extending zend_db_table). I am doing a select and want to have the possibility of excluding a set of ids which which would be passed in as a variable.
I curently have the following:
public function getItemsBySection($section, $excluded_ids=null){
//handle this as special case (although only case at this point)
if(is_array($excluded_ids) and count($excluded_ids)>0){
$x开发者_如何学Go=1;
foreach($excluded_ids as $key=>$value){
if($x==1){
$not_in=$value;
}else{
$not_in.=','.$value;
}
$x++;
}
}
if($section=='feed_supplies'){
$sql='select * from items where is_feed_supply=1';
if($not_in){
$sql.=' and id not in ('.$not_in.')';
}
}
echo $sql . '<br/>';
$results=$this->_db->fetchAll($sql);
but was wondering if there is a way to use Zend_Db_Expr or some other concstruct to handle the exclusion of certain items? I'm not error handling like if it is an integer etc...
I personally am not that big of a fan of the zend_db_select syntax but could be persuaded if it fixes things like this (want to see Doctrine2).
thanks
$select = $db->select();
$select->from('items')
->where('is_feed_supply = ?',1)
->where('id NOT IN (?)', $notInArray);
The $notInArray will be imploded automatically AFAIK (at least for ints). And then
$yourSQL = $select->__toString();
Zend_Db_Select rocks :D
And BTW: Try to stick to coding standarts
I'm not sure about using Zend_Db_Expr, but you don't need that first loop at all to build your $not_in value.
$not_in = implode( ',', $excluded_ids );
精彩评论