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

时间:2014-01-26 09:53:00

标签: php mysql codeigniter

我在使用codeigniters活动记录获取大WHERE语句时遇到问题。

以下是我目前的代码

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;
}

当我调用此函数时,我收到以下错误

Column 'complete' in where clause is ambiguous

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

知道CI为何将posts.complete = 0转换为complete = 0

1 个答案:

答案 0 :(得分:1)

您可以将where()方法的第三个参数设置为FALSE,以防止CI向[{1}} / table个名称添加反引号。

来自 CI doc

  

$ this-> db-> where()接受可选的第三个参数。如果你设置它   为FALSE,CodeIgniter不会尝试保护您的字段或表格   带反叛的名字。

如下:

field