雄辩的大块()缺少一半的结果 [英] Eloquent chunk() missing half the results
问题描述
错过了一些结果。
这是一个测试查询:
$ destinataires = Destinataire :: where('statut','< ,$ 3
- > where('tokenized_at','& foreach($ destinataires as $ destinataire){
$ this-> i ++;
}
}
echo $ this-> i;
它提供124838个结果。
但是:
$ num_dest = Destinataire :: where('statut','<',3)
- > where('tokenized_at' <',$ date_active)
- > count();
echo $ num_dest;
提供249676,所以只有TWICE作为第一个代码示例。
我的脚本应该编辑数据库中的所有匹配记录,如果我启动它多个时间,它每次只剩下一半的剩余记录。
我尝试使用DB :: table()而不是Model。
我试图添加一个 - > take(20000),但似乎没有考虑到。
我用 - > toSql()回显了查询,并且eveything似乎很好(当我添加了 - > take()参数时,添加了LIMIT子句)。
任何建议?
我无法解决这个问题,我不得不停止使用chunk() 。
这是我在使用一个简单的老式PDO时所做的。
注意一件事:PDO填充的$ row变量(obvisouly)不是一个有说服力的模型,而只是一个stdClass对象。
$ pdo = \DB :: connection() - > getPdo();
$ query =SELECT * FROM $ myModelTable WHERE ....;
$ stmt = $ pdo-> prepare($ query);
$ stmt-> execute();
while($ row = $ stmt-> fetchObject()){
// $ row is stdClass而不是MyModel
}
希望这有帮助。我期待着关于这个大块随机奇怪行为的一些信息。
I have a problem with Laravel's ORM Eloquent chunk() method. It misses some results. Here is a test query :
$destinataires = Destinataire::where('statut', '<', 3)
->where('tokenized_at', '<', $date_active)
->chunk($this->chunk, function ($destinataires) {
foreach($destinataires as $destinataire) {
$this->i++;
}
}
echo $this->i;
It gives 124838 results.
But :
$num_dest = Destinataire::where('statut', '<', 3)
->where('tokenized_at', '<', $date_active)
->count();
echo $num_dest;
gives 249676, so just TWICE as the first code example.
My script is supposed to edit all matching records in the database. If I launch it multiple times, it just hands out half the remaining records, each time.
I tried with DB::table() instead of the Model. I tried to add a ->take(20000) but it doesn't seem to be taken into account. I echoed the query with ->toSql() and eveything seems to be fine (the LIMIT clause is added when I add the ->take() parameter).
Any suggestions ?
I couldn't manage to solve this problem, I had to stop using chunk() instead.
Here's what I did to use a plain old PDO while. Be careful of one thing : the $row variable populated by PDO is (obvisouly) not an Eloquent Model but instead just a stdClass object.
$pdo = \DB::connection()->getPdo();
$query = "SELECT * FROM $myModelTable WHERE ....";
$stmt = $pdo->prepare($query);
$stmt->execute();
while ($row = $stmt->fetchObject()) {
// $row is stdClass instead of MyModel
}
Hope this helps. I'm looking forward to some information on that chunk's random strange behavior.
这篇关于雄辩的大块()缺少一半的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!