платформа usgin laravel 5.
С кодом, вставленным ниже, это не работает нормально, так как выделяет слишком много памяти. Все результаты сохраняются в памяти до тех пор, пока я не закончу запись файла (php exec)
Сначала я попробовал (ОЧЕНЬ НЕЭФФЕКТИВНО)< /p>
1. Получение результатов из БД
Код: Выделить всё
$mytable = 'table';
$filePath = storage_path().'/csv/test.csv';
$mins = Carbon::now()->subMinutes(10000);
// set the fetch Assoc mode temoporarly
DB::setFetchMode(PDO::FETCH_ASSOC);
$results = DB::table("$mytable")->where('ts_activity', '>=', $mins)->get();
// revert the fetch Class mode
DB::setFetchMode(PDO::FETCH_CLASS);
Код: Выделить всё
if(count($results)>0){
$headerLine = implode(',',array_keys($results[0]));
dump($headerLine);
if(!file_put_contents($filePath, $headerLine.PHP_EOL)){
throw new Exception("Cannot write FILE: {$filePath}");
}
foreach($results as $row){
foreach($row as $k=>$v){
if($row[$k] == '0000-00-00 00:00:00'){$row[$k] = '';}
$row[$k] = trim(str_replace(array(',', "\n", "\r"), ' ', $row[$k]));
}
//ADDs content to file
if(!file_put_contents($filePath, implode(',', $row).PHP_EOL, FILE_APPEND)){
throw new Exception("Cannot write FILE: {$filePath}");
}
}
//check file size created
$fileSize = filesize($filePath);
$reportString = "Created file at: $filePath of ($fileSize)";
//report
print($reportString);
RETURN TRUE;
}
Код: Выделить всё
//conection:
$link = mysqli_connect(env('DB_HOST'),env('DB_USERNAME'),env('DB_PASSWORD'),env('DB_DATABASE')) or die("Error " . mysqli_error($link));
//consultation:
$query = "SELECT * FROM $mytable WHERE ts_activity>='$mins';" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = mysqli_query($link, $query);
$count = 0;
while($row = $result->fetch_assoc()) {
if($count++==0){
$headerLine = implode(',', array_keys($row));
dump($headerLine);
if(!file_put_contents($filePath, $headerLine.PHP_EOL)){
throw new Exception("Cannot write FILE: {$filePath}");
}
continue;
}
foreach($row as $k=>$v){
if($row[$k] == '0000-00-00 00:00:00'){$row[$k] = '';}
$row[$k] = trim(str_replace(array(',', "\n", "\r"), ' ', $row[$k]));
}
//ADDs content to file
if(!file_put_contents($filePath, implode(',', $row).PHP_EOL, FILE_APPEND)){
throw new Exception("Cannot write FILE: {$filePath}");
}
}
спасибо
Подробнее здесь: https://stackoverflow.com/questions/321 ... ng-laravel