Created
October 6, 2017 10:41
-
-
Save alexlcdee/49931fb449a7f883a627a98367817722 to your computer and use it in GitHub Desktop.
using-batch-query-class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Starts a batch query. | |
* | |
* A batch query supports fetching data in batches, which can keep the memory usage under a limit. | |
* This method will return a [[BatchQueryResult]] object which implements the [[\Iterator]] interface | |
* and can be traversed to retrieve the data in batches. | |
* | |
* For example, | |
* | |
* ```php | |
* foreach (User::model()->batch() as $rows) { | |
* // $rows is an array of 100 or fewer rows from user table | |
* } | |
* ``` | |
* | |
* @param \CDbCriteria|array $criteria | |
* @param int $batchSize the number of records to be fetched in each batch. | |
* @return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface | |
* and can be traversed to retrieve the data in batches. | |
*/ | |
public function batch($criteria = [], $batchSize = 100) | |
{ | |
return new BatchQueryResult($this->getDbConnection(), $batchSize, $this, $criteria, false); | |
} | |
/** | |
* Starts a batch query and retrieves data row by row. | |
* This method is similar to [[batch()]] except that in each iteration of the result, | |
* only one row of data is returned. For example, | |
* | |
* ```php | |
* foreach (User::model()->each() as $row) { | |
* // $row is an instance of ActiveRecord | |
* } | |
* ``` | |
* | |
* @param \CDbCriteria|array $criteria | |
* @param int $batchSize the number of records to be fetched in each batch. | |
* @return BatchQueryResult the batch query result. It implements the [[\Iterator]] interface | |
* and can be traversed to retrieve the data in batches. | |
*/ | |
public function each($criteria = [], $batchSize = 100) | |
{ | |
return new BatchQueryResult($this->getDbConnection(), $batchSize, $this, $criteria, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment