1

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=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

2 Answers 2

4
$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

Sign up to request clarification or add additional context in comments.

1 Comment

Except that Zend_Db_Select does not rock since you had to check $notInArray is not empty. Otherwise, you'll get a SQL syntax error... Great.
1

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 );

1 Comment

agreed - i think i'd just written a foreach loop and it just came out way - certainly tidies it up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.