getPaginationCount()函数在Laravel的雄辩ORM中不起作用

科林·M

我将退出Eloquent模块以单独使用它,但是我可以访问QueryBuilder函数。除了那一个,他们都在工作。

当我运行count()和getPaginationCount()时,count()返回正确的值,但是getPaginationCount()仅返回Illuminate \ Database \ Eloquent \ Builder对象,就好像我没有命令查询要运行一样。但是,我可以在查询日志中看到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

这是查询日志:

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

编辑:

似乎count()forPage()调用使用该函数时,我已经暴露了该函数的一个更一般的错误看一下这些结果:

$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.
贾里克·特卡奇克(Jarek Tkaczyk)

您的代码(和Laravel)有两个问题:

  1. getPaginationCount()在Eloquent Builder上调用会运行方法(在Query Builder类上),但不会返回其结果。而是将自身返回为$this

  2. getPaginationCount()不会重置,limitoffset在查询中获得计数,我想这是一个错误。结果是,null只要将offset设置为大于1的任何值,都forPage($page)返回$page > 1

话虽如此,我建议您要么使用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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章