dqm7854 2014-01-26 09:53
浏览 48
已采纳

复杂where / join语句与codeigniter跨多个表

I am having issues getting a large WHERE statement to function using codeigniters active record.

Below is the code I currently have

public function get_pending_posts($userid){
    $where = "posts.complete = 0 AND poststatuses.contact != 3 AND (poststatuses.poster = $userid OR poststatuses.accepter = $userid)";

    $data = $this->db->select('posts.id AS postid, posts.option, a.gravatar, posts.title,p.firstname AS pfirstname, p.lastname AS plastname, a.firstname AS afirstname, a.lastname AS alastname, poststatuses.contact,poststatuses.modified')
            ->join('users p','poststatuses.poster = p.id')
            ->join('users a','poststatuses.accepter = a.id')
            ->join('posts','poststatuses.postid = posts.id')
            ->where($where)
            ->get('poststatuses');

    $results = array();
    if($data->num_rows()){
        $results['num'] = $data->num_rows();
        $results['requests'] = $data->result();
    }
    else{
        $results['num'] = 0;
    }
    return $results;
}

When I call this function I get the following error

Column 'complete' in where clause is ambiguous

SELECT * FROM (`poststatuses`) JOIN `posts` ON `posts`.`id` = `poststatuses`.`id` WHERE `complete` = 1 AND `rating` = 0

Any idea why CI is converting posts.complete = 0 to complete = 0?

  • 写回答

1条回答 默认 最新

  • douwei7203 2014-01-26 09:59
    关注

    You could set the third parameter of where() method to FALSE to prevent CI from adding backticks to table/field names.

    From CI doc:

    $this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.

    As follows:

    $this->db->where($where, NULL, FALSE);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大