Codeigniter连接返回错误的行

时间:2014-03-31 17:32:52

标签: php sql activerecord join codeigniter-2

我有几天试图弄清问题是什么,但我不能。

我有这个功能:

function gets($status = NULL, $bought = NULL)
{
    $this->db->select('leads.*, clips.id AS clip_id, clips.lead_id AS clip_lead_id, clips.partner_id AS clip_partner_id, clips.type AS clip_type, clips.clip AS clip_clip, clips.price AS clip_price, clips.created_at AS clip_created_at, clips.updated_at AS clip_updated_at, clips.ip_address AS clip_ip_address');
    if ($status != FALSE)
    {
        $this->db->where('leads.status', $status);
    }
    $this->db->from('leads');
    if ($bought != FALSE)
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips', 'clips.lead_id = leads.id');
    }
    else
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips', 'clips.lead_id = leads.id', 'left outer');
    }
    $query = $this->db->get();

    if ($query->num_rows() != 0)
    {
        return $query->result();
    }
}

如果我这样调用函数$ this->得到(' approved');它应返回引导表中的所有行WHERE在表剪辑中找不到匹配,其中partner_id等于当前会话且类型为负。

如果我这样调用这个函数$ this->得到('','买');它应该从发现匹配的引线返回所有行,条件与上述条件相同。

我希望你能理解我的代码并帮助我。

如果您有任何疑问,请随时提出!

谢谢!

2 个答案:

答案 0 :(得分:0)

我认为它应该是这样的

function gets($status = false, $bought = false)
{
    $this->db->select('leads.*, clips.id AS clip_id, clips.lead_id AS clip_lead_id, clips.partner_id AS clip_partner_id, clips.type AS clip_type, clips.clip AS clip_clip, clips.price AS clip_price, clips.created_at AS clip_created_at, clips.updated_at AS clip_updated_at, clips.ip_address AS clip_ip_address');
    if ($status)
    {
        $this->db->where('leads.status', $status);
    }
    if ($bought)
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips clips', 'clips.lead_id = leads.id');
    }
    else
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips clips', 'clips.lead_id = leads.id', 'left outer');
    }
    $query = $this->db->get('leads leads');

    if ($query->num_rows() != 0)
    {
        return $query->result();
    }
}

答案 1 :(得分:0)

我重写了代码,这就是我的工作。

function gets($status = NULL, $bought = NULL)
{
    // Get the rows where the partner has used clips
    $this->db->where('partner_id', $this->session->userdata('partner_id'));
    $this->db->where('type', '-');
    $query = $this->db->get('clips');

    if ($status != FALSE)
    {
        if(is_array($status))
        {
            $first = TRUE;
            foreach($status as $row)
            {
                if ($first == TRUE)
                {
                    $this->db->where('leads.status', $row);
                    $first = FALSE;
                }
                else
                {
                    $this->db->or_where('leads.status', $row);
                }
            }
        }
        else
        {
            $this->db->where('leads.status', $status);
        }
    }

    $bought_array[] = '';
    if ($query->num_rows() != 0)
    {
        foreach($query->result() as $row2)
        {
                    // Add the ID's of the bought leads to array
            $bought_array[] = $row2->lead_id;
        }
    }

    // If bought leads should not be shown in the results
    if ($bought == FALSE)
    {
        $this->db->where_not_in('id', $bought_array);
    }
    else // If they should...
    {
        $this->db->where_in('id', $bought_array);
    }

    $query2 = $this->db->get('leads');

    if ($query2->num_rows() != 0)
    {
        return $query2->result();
    }
}