To read the current row of data, call DataReader::read. The method DataReader::readAll
returns all the rows in a single array. Rows of data can also be read by
iterating through the reader. For example,
php
$command = $connection->createCommand('SELECT * FROM post');
$reader = $command->query();
while ($row = $reader->read()) {
$rows[] = $row;
}
equivalent to:
foreach ($reader as $row) {
$rows[] = $row;
}
equivalent to:
$rows = $reader->readAll();
Note that since DataReader is a forward-only stream, you can only traverse it once.
Doing it the second time will throw an exception.
It is possible to use a specific mode of data fetching by setting
[[fetchMode]]. See the PHP manual
for more details about possible fetch mode.
/** * Returns an object populated with the next row of data. * @param string $className class name of the object to be created and populated * @param array $fields Elements of this array are passed to the constructor * @return mixed the populated object, false if no more row of data available */ public function readObject($className, $fields) { //Try..Catch to prevent Function sequence error: -11067 try { return parent::readObject($className, $fields); } catch (\Exception $ex) { } return false; }
/** * Returns an object populated with the next row of data. * @param string $className class name of the object to be created and populated * @param array $fields Elements of this array are passed to the constructor * @return mixed the populated object, false if no more row of data available */ public function readObject($className, $fields) { //Try..Catch to prevent CLI0125E Function sequence error. SQLSTATE=HY010 try { return parent::readObject($className, $fields); } catch (\Exception $ex) { } return false; }
/** * Renders next batch of rows. * @param integer $rowsNumber set to -1 to render all rows * @return string */ public function renderChunk($rowsNumber) { /** @var ActiveDataProvider $dataProvider */ $dataProvider = $this->grid->dataProvider; /** @var \yii\db\ActiveQuery $query */ $query = $dataProvider->query; $result = ''; if ($this->rowNumber === 0) { $result .= $this->renderHeader(); } $rowCount = $this->dataReader->getRowCount(); for ($i = 0; $i < $rowsNumber || $rowsNumber === -1; $i++) { $row = $this->dataReader->read(); if ($row === false) { $this->dataReader = null; $result .= $this->renderFooter(); break; } $models = $query->populate([$row]); $result .= $this->renderRow(reset($models), $this->rowNumber++, $rowCount); } return $result; }