代码之家  ›  专栏  ›  技术社区  ›  timpone

Zend_-db适配器不在/Zend_-db_-expr中

  •  1
  • timpone  · 技术社区  · 15 年前

    我有一个对模型的调用,它只是一个标准类(不扩展zend_db_表)。我正在进行选择,希望能够排除一组将作为变量传入的ID。

    我现在有以下几点:

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

    但是想知道是否有一种方法可以使用zend-db-expr或其他construct来处理某些项的排除?我不会像处理整数那样处理错误…

    我个人并不是Zend_-DB_-Select语法的狂热粉丝,但如果它能解决这样的问题(想看教义2),我会被说服的。

    谢谢

    2 回复  |  直到 15 年前
        1
  •  1
  •   Peter Bailey    15 年前

    我不确定是否使用zend_-db_-expr,但您根本不需要第一个循环来构建您的非价值美元。

    $not_in = implode( ',', $excluded_ids );
    
        2
  •  4
  •   TomáÅ¡ Fejfar    15 年前
    $select = $db->select();
    $select->from('items')
           ->where('is_feed_supply = ?',1)
           ->where('id NOT IN (?)', $notInArray);
    

    $notinarray将自动内爆afaik(至少对于ints)。然后

    $yourSQL = $select->__toString();
    

    Zend_DB_选择岩石:d

    顺便说一句:尽量坚持编码标准