使用查询构建器进行 Kohana 3 跨表更新

标签 kohana kohana-3 kohana-db

使用数据库查询构建器在 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/

相关文章:

mysql - 以原始数据类型从数据库返回值 (Kohana ORM)

kohana - 如何在 Kohana ORM 中使用 BETWEEN 子句

php - 在 MVC 框架中从哪里发送邮件,这样就没有重复代码?

Kohana ORM 关系问题

php - Kohana 3.2 session 过早过期,较短的过期时间按预期工作

MySql 函数与 kohana ORM

php - 如何选择vote_points最多的数据库记录并确保它最先显示? PHP/科哈纳 3

php - 在 Kohana 加载和调用外部库

php - fatal error : Cannot re-assign auto-global variable _POST in kohana 3. 2