在同一个Controller函数中执行不同查询的问题

时间:2018-03-28 08:34:43

标签: php mysql codeigniter activerecord

这里我有函数index,它有两个不同的模型调用,如下所示:
控制器:

function index()
{
    $people = $this->M_results->search_people(); // first model method call 
    // print_r($people); //shows result
    $skills = $this->M_results->get_skill($category_name); // second model method call 
}  

模特:

function search_people()
{
    $this->db->select("registration,gender,profile_img,location");
    $this->db->from('search_result');
    $this->db->join('services','search_result.registration = services.reg_id','left');
    $this->db->join('review','search_result.registration = review.reg_id AND review.active = 0','left');
    $query = $this->db->get();
    $result = $query->result_array();
    return $result;
}

function get_skill($category_name)
{
    $this->db->select('GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id,GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill,sub_categories.name as sub_cat,class');
    $this->db->from('skills');
    $this->db->join('sub_categories','skills.sub_cat_id = sub_categories.id');
    $this->db->join('categories','sub_categories.cat_id = categories.id');
    $this->db->where('categories.name',$category_name);
    $this->db->where('skills.active',0);
    $this->db->group_by('sub_categories.name');
    // echo $this->db->get_compiled_select(); exit();
    $query = $this->db->get();
    return  $query->result();
}  

现在的问题是,对search_people的调用会返回准确的结果。

但是在运行第二个模型调用,即get_skill函数时,get_skill的选择查询包括search_result函数中的select查询列。并显示以下数据库错误:

  

错误号码:1054

     

“字段列表”中的未知列'注册'

     

选择注册,性别,profile_img,位置,GROUP_CONCAT(skills.id ORDER BY skills.name ASC)作为id,GROUP_CONCAT(skills.name ORDER BY skills.name ASC)作为技能,sub_categories.name作为sub_cat,类FROM技能JOIN sub_categories ON skills.sub_cat_id = sub_categories.id JOIN类别ON sub_categories.cat_id = categories.id WHERE categories.name ='Child and Pet Care'AND skills.active = 0 GROUP BY sub_categories.name

在上面的查询中,您可以在search_peopleget_skill中看到这两列。

1 个答案:

答案 0 :(得分:0)

发布未来参考的答案。

重置当前的Query Builder状态。当您想要构建可在特定条件下取消的查询时很有用。

$this->db->reset_query();

我从下面链接找到了解决方案:
https://www.codeigniter.com/userguide3/database/query_builder.html#CI_DB_query_builder::reset_query