2012-07-03 51 views
2

使用DB查询生成器在Kohana 3中构建交叉表更新的正确方法是什么?Kohana 3使用查询生成器进行交叉表更新

目前我只是使用DB :: expr,但我知道查询生成器比这更聪明。

// update record 
$rows_updated = DB::update(DB::expr('user_list_permissions INNER JOIN users ON user_list_permissions.user_id = users.id')) 
->set($params) 
->where('user_list_permissions.id', '=', $user_list_permission_id) 
->where('users.account_id', '=', $this->account_id) 
->execute(); 

是的,当然我试图用建立SELECT查询时像“加盟”的方法,但我收到一个错误:

ErrorException [ 1 ]: Call to undefined method Database_Query_Builder_Update::join() 

回答

2

所以,你正在使用的表达做加盟,它可以在'on'函数中使用内置的'join'函数来实现这种行为。

所以,在你的榜样它看起来是这样的:

$rows_updated = DB::update('user_list_permissions') 
->join('users','INNER') 
->on('user_list_permissions.user_id','=','users.id') 
->set($params) 
->where('user_list_permissions.id', '=', $user_list_permission_id) 
->where('users.account_id', '=', $this->account_id) 
->execute(); 

没有太多就可以了但该文档确实有在http://kohanaframework.org/3.2/guide/database/query/builder#joins

+0

不,不适用于更新。来自服务器的响应:ErrorException [1]:调用未定义的方法Database_Query_Builder_Update :: join() – JoshuaDavid

+0

从3.1开始,“join”方法是否添加到Database_Query_Builder_Update对象?我在v3.1.3.1(araea)中测试 – JoshuaDavid

+0

v.3.3.3在Database_Query_Builder_Update上没有连接方法。 – Zoon

0

一点点这是一个老的文章,但只是为了保持我在Kohana的经历记录。

如果你正在使用MySQL,它可以让你进行跨表更新避免使用加入如下的:

UPDATE table1, table2 
SET table1.some_field = 'some_value' 
WHERE table1.foreign_key = table2.primary_key AND table2.other_field = 'other_value' 

注意条件table1.foreign_key = table2.primary_key是一样的你在JOIN中使用ON子句。因此,您可以在Kohana中编写一个交叉表更新,并遵循该模式避免JOIN:

$rows_updated = DB::update(DB::expr('user_list_permissions, users')) 
->set($params) 
->where('user_list_permissions.user_id', '=', DB::expr('users.id')) 
->where('user_list_permissions.id', '=', $user_list_permission_id) 
->where('users.account_id', '=', $this->account_id) 
->execute();