如何在Codeigniter活动记录中使用where_not_in()和两个条件?

时间:2016-08-22 10:55:37

标签: php codeigniter activerecord

我使用活动记录在codeigniter中编写了一个搜索功能。我想要做的是,如果payment_type = 5并且在同一记录order_status = 1,那么我想省略它。但是可以有记录,其中payment_type = 5和order_status = 2.如果且仅满足两个条件则必须省略该记录。 (payment_type = 5 AND order_status = 1)。我在下面添加了模型功能。我想知道如何使用活动记录来做到这一点。如果有人有任何想法会非常有帮助。提前谢谢。

public function getSalesByStore($from,$to,$status,$filter_stores,$offset, $amount, $limit = '')
    {
        $result = array();
        $this->db->select("*,a.created AS order_created,store_name, users.name, a.id as orderid, a.payment_type AS payment");
        $this->db->from("$this->table a");
        $this->db->join('users', "a.customer_id = users.id");
        $this->db->join('stores', "a.store_id = stores.id");
        $this->db->order_by("a.id", "desc");  


        if(!empty($status))
        {
            if ($status == 6)
            {
                $this->db->where('order_status != 3');
            }
            elseif ($status == 1) 
            {
                $this->db->where("a.payment_type != 5 AND order_status = $status");
            }
            else
            {
                $this->db->where('order_status', $status);    
            }
        }

        if(!empty($filter_stores))
        {
            $this->db->where('store_id in ('.$filter_stores.')');
        }
        if(!empty($from))
        {
            $this->db->where('date(a.created) >=', date($from));
        }
        if(!empty($to))
        {
            $this->db->where('date(a.created) <=', date($to));
        }
        if(!empty($amount))
        {
            if ($amount == 1)
            {
                $this->db->where("a.total_amount <=", 50);
            }else if ($amount == 2)
            {
                $this->db->where("a.total_amount >=", 50);    
            }      
        }
        $this->db->limit($limit, $offset);
        $query = $this->db->get();
        if ($query->num_rows() > 0) 
        {
            foreach ($query->result() as $row)
            {
                $row->order_status = $this->get_status($row->order_status);
                $result [] = $row;
            }
        }

        return $result;

    }

2 个答案:

答案 0 :(得分:1)

使用此代码

<?php
public function getSalesByStore($from,$to,$status,$filter_stores,$offset, $amount, $limit = '')
    {
        $result = array();
        $this->db->select("*,a.created AS order_created,store_name, users.name, a.id as orderid, a.payment_type AS payment");
        $this->db->from("$this->table a");
        $this->db->join('users', "a.customer_id = users.id");
        $this->db->join('stores', "a.store_id = stores.id");
        $this->db->order_by("a.id", "desc");  


        if(!empty($status))
        {
            if ($status == 6)
            {
                //$this->db->where('order_status != 3');

                $this->db->where_not_in("order_status",'3');
            }
            elseif ($status == 1) 
            {
               // $this->db->where("a.payment_type != 5 AND order_status = $status");

                 $this->db->where("order_status","$status");
                $this->db->where_not_in("a.payment_type",'5');
            }
            else
            {
                $this->db->where('order_status', $status);    
            }
        }

        if(!empty($filter_stores))
        {
           // $this->db->where('store_id in ('.$filter_stores.')');

           $this->db->where_in('store_id',"$filter_stores");
        }
        if(!empty($from))
        {
            $this->db->where('date(a.created) >=', date($from));


        }
        if(!empty($to))
        {
            $this->db->where('date(a.created) <=', date($to));
        }
        if(!empty($amount))
        {
            if ($amount == 1)
            {
                $this->db->where("a.total_amount <=", 50);
            }else if ($amount == 2)
            {
                $this->db->where("a.total_amount >=", 50);    
            }      
        }
        $this->db->limit($limit, $offset);
        $query = $this->db->get();
        if ($query->num_rows() > 0) 
        {
            foreach ($query->result() as $row)
            {
                $row->order_status = $this->get_status($row->order_status);
                $result [] = $row;
            }
        }

        return $result;

    }
?>  

答案 1 :(得分:1)

我没有检查过此代码,但这可能是您正在寻找的答案

public function getSalesByStore($from,$to,$status,$filter_stores,$offset, $amount, $limit = '')
{
    $result = array();
    $this->db->select("*,a.created AS order_created,store_name, users.name, a.id as orderid, a.payment_type AS payment");
    $this->db->from("$this->table a");
    $this->db->join('users', "a.customer_id = users.id");
    $this->db->join('stores', "a.store_id = stores.id");
    $this->db->order_by("a.id", "desc");
    $this->db->where_not_in("a.payment_type",5); 
    $this->db->where_not_in("order_status",1); 


    if(!empty($status) && !empty($filter_stores)){
        if ($status == 6){
            $this->db->where_not_in('order_status',3);
        }
        else{
            $this->db->where('order_status', $status);    
        }
    }
    if(!empty($filter_stores)){
        $this->db->where('store_id in ('.$filter_stores.')');
    }
    if(!empty($from)){
        $this->db->where('date(a.created) >=', date($from));
    }
    if(!empty($to)){
        $this->db->where('date(a.created) <=', date($to));
    }
    if(!empty($amount)){
        if ($amount == 1){
            $this->db->where("a.total_amount <=", 50);
        }else if ($amount == 2){
            $this->db->where("a.total_amount >=", 50);    
        }      
    }
    $this->db->limit($limit, $offset);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row){
            $row->order_status = $this->get_status($row->order_status);
            $result [] = $row;
        }
    }
    return $result;
}