[英]getPaginationCount() function not working in Laravel's Eloquent ORM
I'm pulling out the Eloquent module to use it separately, but I have access to the QueryBuilder functions. 我将退出Eloquent模块以单独使用它,但是我可以访问QueryBuilder函数。 They're all working except for that one.
除了那一个,他们都在工作。
When I run a count() and a getPaginationCount(), the count() returns the correct value, but getPaginationCount() just returns the Illuminate\\Database\\Eloquent\\Builder object, as if I didn't command a query to run. 当我运行count()和getPaginationCount()时,count()返回正确的值,但是getPaginationCount()仅返回Illuminate \\ Database \\ Eloquent \\ Builder对象,就好像我没有命令查询要运行一样。 However, I can see 2 queries in the query log, and oddly, they both run the same query.
但是,我可以在查询日志中看到2个查询,而且奇怪的是,它们都运行相同的查询。
require 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection(array(
'driver' => 'mysql',
'host' => TECHDB_HOST,
'database' => TECHDB_DBNAME,
'username' => TECHDB_USER,
'password' => TECHDB_PASS,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
));
$capsule->bootEloquent();
class Indicator extends Illuminate\Database\Eloquent\Model {
public $timestamps = false;
public $table = "indicator_performance";
}
$query = Indicator::where('symbol', '=', 'IBM')->forPage(1,2);
var_dump($query->count()); //Correctly prints '13'
var_dump($query->getPaginationCount()); //dumps the 'Illuminate\Database\Eloquent\Builder' object
Here is the query log: 这是查询日志:
array (size=2)
0 =>
array (size=3)
'query' => string 'select count(*) as aggregate from `indicator_performance` where `symbol` = ? limit 2 offset 0' (length=93)
'bindings' =>
array (size=1)
0 => string 'IBM' (length=3)
'time' => float 4.85
1 =>
array (size=3)
'query' => string 'select count(*) as aggregate from `indicator_performance` where `symbol` = ? limit 2 offset 0' (length=93)
'bindings' =>
array (size=1)
0 => string 'IBM' (length=3)
'time' => float 4.24
EDIT: 编辑:
It seems I've exposed a more general bug with the count()
function when used after a forPage()
call. 似乎在
forPage()
调用之后使用count()
函数暴露了一个更一般的错误。 Have a look at these results: 看一下这些结果:
$query = Indicator::where('symbol', '=', 'IBM');
var_dump($query->count()); //Correctly returns '13'
var_dump($query->forPage(0, 5)->count()); //Correctly returns '13'
var_dump($query->forPage(1, 5)->count()); //Correctly returns '13'
var_dump($query->forPage(2, 5)->count()); //Returns null. Should return '13' or '3', depending on implementation.
There are 2 problems with your code (and the Laravel): 您的代码(和Laravel)有两个问题:
getPaginationCount()
called on the Eloquent Builder runs the method (on the Query Builder class) but doesn't return its result. getPaginationCount()
Builder上调用的getPaginationCount()
运行方法(在Query Builder类上),但不返回其结果。 Instead it returns itself as $this
. 相反,它将自身返回为
$this
。
getPaginationCount()
doesn't reset limit
and offset
on the query to get the count, which I suppose is a bug. getPaginationCount()
不会重置查询的limit
和offset
以获得计数,我想这是一个错误。 This results in returning null
whenever offset is set to anything higher than 1. forPage($page)
will do that for $page > 1
. 每当将offset设置为大于1的任何
null
时,这都会导致返回forPage($page)
将对$page > 1
。
That being said, I suggest you either use count()
instead of getPaginationCount()
or: 话虽如此,我建议您要么使用
count()
而不是getPaginationCount()
要么:
// first get the count
$count = $query->getQuery() // get the base Query Builder directly
->getPaginationCount(); // in order to return the count
// then use forPage
$query->forPage(1,5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.