별도로 사용하기 위해 Eloquent 모듈을 꺼내고 있지만 QueryBuilder 함수에 액세스 할 수 있습니다. 그것들을 제외하고는 모두 작동합니다.
count () 및 getPaginationCount ()를 실행하면 count ()는 올바른 값을 반환하지만 getPaginationCount ()는 쿼리를 실행하도록 명령하지 않은 것처럼 Illuminate \ Database \ Eloquent \ Builder 객체 만 반환합니다. 그러나 쿼리 로그에서 두 개의 쿼리를 볼 수 있으며 이상하게도 둘 다 동일한 쿼리를 실행합니다.
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.
코드 (및 Laravel)에는 두 가지 문제가 있습니다.
getPaginationCount()
Eloquent Builder에서 호출 된 메서드는 Query Builder 클래스에서 메서드를 실행하지만 결과를 반환하지 않습니다. 대신 $this
.
getPaginationCount()
재설정하지 않습니다 limit
및 offset
쿼리에 나는 버그가 가정 수를 얻을 수 있습니다. 결과적으로 null
오프셋이 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] 삭제
몇 마디 만하겠습니다