Codeigniter 2 $ this-> db-> join与$ this-> db-> update一起使用 [英] Codeigniter 2 $this->db->join used with $this->db->update

查看:83
本文介绍了Codeigniter 2 $ this-> db-> join与$ this-> db-> update一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到您不能使用:

I just realized that you cannot use:

$this->db->join()

使用

$this->db->update()

似乎连接"是由codeigniter执行的,但未在查询中使用,对通过$ this-> db-> last_query();获得的基表进行更新后可见.

It seems the "join" is executed by codeigniter alright, but is not used in the query, seen after an update on the base table obtained with: $this->db->last_query();

我看到没有加入.然后,我尝试对连接表进行更新,以为仅在需要时才使用连接,但我没有工作,并告诉我错误1054"where子句中的未知列XXX".

I saw that there was no join. Then I tried an update on the joined table thinking the join would only been used if needed, but I did not work and told me the error 1054 "Unknown column XXX in where clause".

有没有办法强制使用代码点火器?在构建软件的方式中,我真的不想自己构建查询的所有不同部分(联接,位置)并调用$ this-> db-> query().

Is there a way to force codeigniter? The way I built my software, I REALLY don't want to build all the different parts of the queries myself (join, where) and the call $this->db->query().

注意:我看到了以下链接:

NOTE: I saw these links:

带有连接的Codeigniter活动记录更新语句

是否可以更新使用Codeigniter的活动记录创建联接表?

codeigniter-数据库:如何使用一个更新查询来更新多个表

但是如果有人知道更简洁的方法,那将是很好的,因为这些解决方案不适用于我的情况,因为我在"preProcessing()"方法中使用了相同的联接,该联接使用这些联接来预览更改,然后使用相同的"preProcessing()"方法进行替换

but if someone knows a cleaner way it would be nice, because these solutions are not working with my case, since I was using the same joins in a "preProcessing()" method that uses the joins for a preview of the changes, then the same "preProcessing()" method is used for the replacement

推荐答案

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

Ok well I managed to find a "clean" solution, using codeigniter's join, set, etc. So what's cool is that you will have all CI's benefits of using $this->db->join(), $this->db->join(), etc. like escaping and adding quotes.

那么首先要做所有您的CI事情:

So first do all your CI stuff:

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

然后,您可以使用Active Record的就绪,已清理和转义的查询元素来构建查询:

Then you can build the query using Active Record's ready, cleaned and escaped query elements:

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

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

If someone has a better solution, I will gladly accept it and use it instead

这篇关于Codeigniter 2 $ this-> db-> join与$ this-> db-> update一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆