使用数据库查询构建器在 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();
是的,我当然尝试使用“join”方法,就像构建 SELECT 查询时一样,但我收到一个错误:
ErrorException [ 1 ]: Call to undefined method Database_Query_Builder_Update::join()
最佳答案
因此,您使用表达式进行连接,可以在“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
关于使用查询构建器进行 Kohana 3 跨表更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314673/