public batch ( integer $batchSize = 100, |
||
$batchSize | integer | the number of records to be fetched in each batch. |
$db | the database connection. If not set, the "db" application component will be used. | |
Результат | the batch query result. It implements the [[\Iterator]] interface and can be traversed to retrieve the data in batches. |
public function batch($batchSize = 100, $db = null)
{
return Yii::createObject(['class' => BatchQueryResult::className(), 'query' => $this, 'batchSize' => $batchSize, 'db' => $db, 'each' => false]);
}
/** * Perform a Database Query and show the result as CSV * * By default, write to the output buffer. * But, if you set the filename, the file will be downloadable. * * @param string $filename Name of the file to download */ public function performAsCSV($filename = null) { // Create the CSV into memory $csv = Writer::createFromFileObject(new SplTempFileObject()); // Insert fields names as the CSV header $csv->insertOne($this->getCSVHeader()); // Print to the output stream foreach ($this->query->batch() as $items) { // $items is an array of 100 or fewer rows from the db table $csv->insertAll($items); } $csv->output($filename); }