firestore(); $ultimaFecha = null; $limite = 1; // 1000; $continuar = true; do { $ref = $firestore->collection("eventos") // ->where("sincronizado", "==", null) ->orderBy("timestamp", "ASC") ->limit($limite); if($ultimaFecha !== null) { $ref = $ref->startAt([$ultimaFecha]); } $c = 0; # Contador de registros procesados foreach($ref->documents() as $doc) { $c++; $data = $doc->data(); $ultimaFecha = $data["timestamp"]; $modelo = Evento::findOne(["firebaseId" => $doc->id()]); if($modelo === null) { $modelo = new Evento(); } try { $modelo->nombre = $data["nombre"]; $modelo->descripcion = $data["descripcion"]; // Mapear información de los modelos $hoy = new \DateTime(); $doc->reference() ->update([ ["path" => "sincronizado", "value" => "OK"], ["path" => "sincronizadoFecha", "value" => $hoy->format("Y-m-d H:i:s")], ["path" => "evidencias", "value" => $data["evidencias"]], ]); } catch(\Exception $e) { $this->stdout("Exception: {$e->getMessage()}\n"); $doc->reference() ->update([ ["path" => "sincronizado", "value" => "ERROR"], ["path" => "sincronizadoError", "value" => $e->getMessage()], ["path" => "sincronizadoErrorLine", "value" => $e->getLine()], ]); } } # Si los registros procesados son menores al límite, terminar if($c < $limite) { $continuar = false; } } while($continuar); } }