首页 文章

Codeigniter $ this-> db-> join与$ this-> db-> update一起使用

提问于
浏览
4

我才意识到你不能使用:

$this->db->join()

$this->db->update()

似乎“join”由codeigniter执行,但是在查询中没有使用,在使用以下参数获得的基表更新后看到:$ this-> db-> last_query();

我看到没有加入 . 然后我尝试了对连接表的更新,认为只有在需要时才会使用连接,但是我没有工作并告诉我错误1054“where where子句中的未知列XXX” .

有没有办法强制使用codeigniter?我构建软件的方式,我真的不想自己构建查询的所有不同部分(join,where)和调用$ this-> db-> query() .

注意:我看到这些链接:

Codeigniter active record update statement with a join

Is it possible to UPDATE a JOINed table using Codeigniter's Active Record?

codeigniter - database : how to update multiple tables with a single update query

但是如果有人知道一种更干净的方式会很好,因为这些解决方案不适用于我的情况,因为我在“preProcessing()”方法中使用相同的连接,该方法使用连接来预览更改,然后相同的“preProcessing()”方法用于替换

1 回答

  • 1

    好吧,我设法找到一个“干净”的解决方案,使用codeigniter的join,set等 . 所以很酷的是你将拥有使用$ this-> db-> join(),$ this-> db-的所有CI的好处 . > join()等,如转义和添加引号 .

    首先要做你所有的CI内容:

    $this->db->join(..) // Set all your JOINs
    $this->db->set(..) // Set your SET data
    $this->db->where(..) // Set all your WHEREs
    

    然后,您可以使用Active Record的ready,clean和escapeped查询元素构建查询:

    // JOIN
    $sql = "UPDATE $this->baseTable ";
    $sql .= implode(' ', $this->db->ar_join);
    
    // SET
    $sql .= ' SET';
    $setArray = array();
    foreach ($this->db->ar_set as $column=>$newValue)
        array_push($setArray, " $column = $newValue");
    $sql .= implode(',', $setArray);
    
    // WHERE
    $sql .= ' WHERE '.implode(' ', $this->db->ar_where);
    
    $this->db->query($sql);
    

    如果有人有更好的解决方案,我会很乐意接受并使用它

相关问题