简体   繁体   English

跨多个表的带有代码点火器的复杂where / join语句

[英]Complex where/join statement with codeigniter across several tables

I am having issues getting a large WHERE statement to function using codeigniters active record. 我遇到了使大型WHERE语句使用codeigniters活动记录起作用的问题。

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 ? 知道为什么CI posts.complete = 0转换为complete = 0吗?

You could set the third parameter of where() method to FALSE to prevent CI from adding backticks to table / field names. 您可以将where()方法的第三个参数设置为FALSE以防止CI将反引号添加到table / field名称中。

From CI doc : CI doc

$this->db->where() accepts an optional third parameter. $ this-> db-> where()接受可选的第三个参数。 If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. 如果将其设置为FALSE,则CodeIgniter不会尝试使用反引号保护您的字段或表名。

As follows: 如下:

$this->db->where($where, NULL, FALSE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM