php - Laravel 5.3 - 以 orWhere 开头的查询生成器

标签 php laravel laravel-5.2 query-builder laravel-5.3

最近 Laravel 发布了 5.3 版本。

下面链接有5.2到5.3的一些升级说明: https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0

Eloquent scopes now respect the leading boolean of scope constraints. For example, if you are starting your scope with an orWhere constraint it will no longer be converted to normal where. If you were relying on this feature (e.g. adding multiple orWhere constraints within a loop), you should verify that the first condition is a normal where to avoid any boolean logic issues.

If your scopes begin with where constraints no action is required. Remember, you can verify your query SQL using the toSql method of a query:

这似乎让事情变得有点困难,升级只会增加限制。

我们有一个内部包,为了使代码尽可能简单和整洁,有一部分依赖于使用 orWhere() 启动查询,并利用它以及递归方法中的闭包概念.如您所知,递归方法最好保持简短。

按照现在的升级说明,在Laravel 5.3上肯定会失败

我只是想知道是否有人知道删除此行为的原因?

谢谢!

更新:

我已经将我们的系统迁移到 Laravel 5.3。我确认这仅在 Eloquent 构建器中受到影响,而不在查询构建器(以前称为“Fluent Builder”)中受到影响。

即使是提交(这是一个 hell 般的大量更改)也只在 Eloquent\Builder 上。 https://github.com/laravel/framework/issues/14829

无论如何,为了安全起见,我确实建议进行相应的调整。这就是我们所做的,尽管我们知道它并没有破坏我们的代码(截至今天)。

最佳答案

我相信原因正是这个拉取请求:https://github.com/laravel/framework/pull/12918

有人想像这样使用作用域:

User::approved()->orActive();

在这种情况下,如果 orActive 范围定义如下:

public function scopeOrActive($q) {
   return $q->orWhere('active',1)
}

approved范围定义如下:

public function scopeApproved($q) {
   return $q->where('approved',1)
}

在 laravel 5.2 中它会解析为:

where approved = 1 AND active = 1

在 Laravel 5.3 中它被解析为:

where approved = 1 OR active = 1

所以这是有道理的,但我从未使用过以 orWhere 开头的范围

关于php - Laravel 5.3 - 以 orWhere 开头的查询生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38357676/

相关文章:

javascript - 从输入中获取数据并在提交按钮上单击控制台记录数据

php - 如何测试 PHP PDO 单例类?

php - 如何对此上下文建模,以便有可能创建自定义问题并收集答案?

php - 为什么/user/{username} 没有自动显示?

php - 每页一个测验项目(php/mysql 测验程序)

php - Windows Azure 中来自 PHP 的电子邮件

php - Laravel 5 API 路线

php - Laravel 5.4 查询生成器使用 orWhere 子句时缺少参数 2

php - Laravel 5 - withInput 在与重定向一起使用时不起作用

php - Laravel 5 中的钩子(Hook)?