我正在尝试根据志愿者的活跃用户以及他们自愿从事的工作在志愿者数据库中创建用户列表。

问题是,它还包括不活动的成员。

这是我的控制器

function View_Vol_LIST()
{
$data = array();

    if($query = $this->report_model->Volunteer_list())
    {
        $data['records'] = $query;
    }
$data['main_content'] = 'volunteer_list';
$this->load->view('includes/template', $data);
}


这是我的模特

function Volunteer_list()
{
    $query = $this->db->order_by('Last_Name', "ASC");
    $query = $this->db->order_by('First_Name', "ASC");
    $query = $this->db->where('Membership_Status !=', 'DNR');
    $query = $this->db->where('Vol_FA =', 'yes');
    $query = $this->db->or_where('Vol_TEL =', 'yes');
    $query = $this->db->or_where('Vol_CD =', 'yes');
    $query = $this->db->or_where('Vol_SCBA =', 'yes');
    $query = $this->db->or_where('Vol_MAIL =', 'yes');
    $query = $this->db->or_where('Vol_SML =', 'yes');
    $query = $this->db->or_where('Vol_BODCOM =', 'yes');
    $query = $this->db->or_where('Vol_OTHER =', 'yes');
    $query = $this->db->get('Membership');
    return $query->result();


似乎or_where完全覆盖了where!= DNR(未续订)

救命!

谢谢!

最佳答案

尝试执行此操作,因为Codeigniter中不允许在Active Record中使用类似嵌套的查询

public function Volunteer_list(){
    $this->db->order_by('Last_Name', "ASC");
    $this->db->order_by('First_Name', "ASC");
    $this->db->where('Membership_Status !=', 'DNR');
    $this->db->where("(Vol_FA='yes'
                       OR Vol_TEL = 'yes'
                       OR Vol_CD='yes'
                       OR Vol_SCBA='yes'
                       OR Vol_MAIL='yes'
                       OR Vol_SML='yes'
                       OR Vol_BODCOM ='yes'
                       OR Vol_OTHER ='yes')", NULL, FALSE);
    $query = $this->db->get('Membership');
    return $query->result();
}


NULL是该查询中的value参数,而FALSE用于删除查询引号。

这将产生以下结果:

SELECT * FROM (`Membership`)
WHERE `Membership_Status` != 'DNR'
AND (Vol_FA='yes'
     OR Vol_TEL = 'yes'
     OR Vol_CD='yes'
     OR Vol_SCBA='yes'
     OR Vol_MAIL='yes'
     OR Vol_SML='yes'
     OR Vol_BODCOM ='yes'
     OR Vol_OTHER ='yes')
ORDER BY `Last_Name` ASC, `First_Name` ASC

10-07 21:20