Q:

Complex where/join statement with codeigniter across several tables

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?

A:

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

