BatchQueryResult
API reference › yii › db › BatchQueryResult
BatchQueryResult represents a batch query from which you can retrieve data in batches.
You usually do not instantiate BatchQueryResult directly. Instead, you obtain it by
calling Query::batch() or Query::each(). Because BatchQueryResult implements the Iterator interface,
you can iterate it to obtain a batch of data in each iteration. For example,
$query = (new Query)->from('user');foreach ($query->batch() as $i => $users) { // $users represents the rows in the $i-th batch}foreach ($query->each() as $user) {}Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$batchSize | int | The number of rows to be returned in each batch. Default: 100. | BatchQueryResult |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$db | Connection| | The DB connection to be used when performing batch query. Default: null. | BatchQueryResult |
$each | bool | Whether to return a single row during each iteration. Default: false. | BatchQueryResult |
$query | Query | The query object associated with this batch query. Default: null. | BatchQueryResult |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__destruct() | Destructor. | BatchQueryResult |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
__wakeup() | Unserialization is disabled to prevent remote code execution in case application calls unserialize() on user input containing specially crafted string. | BatchQueryResult |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
current() | Returns the current dataset. | BatchQueryResult |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
key() | Returns the index of the current dataset. | BatchQueryResult |
next() | Moves the internal pointer to the next dataset. | BatchQueryResult |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
reset() | Resets the batch query. | BatchQueryResult |
rewind() | Resets the iterator to the initial state. | BatchQueryResult |
trigger() | Triggers an event. | yii\ |
valid() | Returns whether there is a valid dataset at the current position. | BatchQueryResult |
fetchData() protected | Fetches the next batch of data. | BatchQueryResult |
getRows() protected | Reads and collects rows for batch | BatchQueryResult |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_RESET | yii\ | An event that is triggered when the batch query is reset. Event name: 'reset'. Available since 2.0.41. | BatchQueryResult |
EVENT_FINISH | yii\ | An event that is triggered when the last batch has been fetched. Event name: 'finish'. Available since 2.0.41. | BatchQueryResult |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
MSSQL_NO_MORE_ROWS_ERROR_CODE | -13 | MSSQL error code for exception that is thrown when last batch is size less than specified batch size | BatchQueryResult |
Property details
Section titled “Property details”public yii\db\Connection|null $db = nullThe DB connection to be used when performing batch query.
If null, the “db” application component will be used.
$each
Section titled “$each ”public bool $each = falseWhether to return a single row during each iteration.
If false, a whole batch of rows will be returned in each iteration.
$query
Section titled “$query ”public yii\db\Query $query = nullThe query object associated with this batch query.
Do not modify this property directly unless after reset() is called explicitly.
Method details
Section titled “Method details”__destruct()
Section titled “__destruct() ”public function __destruct(): mixedDestructor.
Returns
mixed
__wakeup()
Section titled “__wakeup() ”public function __wakeup(): mixedUnserialization is disabled to prevent remote code execution in case application calls unserialize() on user input containing specially crafted string.
current()
Section titled “current() ”public function current(): yii\db\TValueReturns the current dataset.
This method is required by the interface Iterator.
Returns
yii\db\TValue: The current dataset.
key()
Section titled “key() ”public function key(): yii\db\TKey|nullReturns the index of the current dataset.
This method is required by the interface Iterator.
Returns
yii\db\TKey|null: The index of the current row.
next()
Section titled “next() ”public function next(): mixedMoves the internal pointer to the next dataset.
This method is required by the interface Iterator.
Returns
mixed
reset()
Section titled “reset() ”public function reset(): mixedResets the batch query.
This method will clean up the existing batch query so that a new batch query can be performed.
Returns
mixed
rewind()
Section titled “rewind() ”public function rewind(): mixedResets the iterator to the initial state.
This method is required by the interface Iterator.
Returns
mixed
valid()
Section titled “valid() ”public function valid(): boolReturns whether there is a valid dataset at the current position.
This method is required by the interface Iterator.
Returns
bool: Whether there is a valid dataset at the current position.
fetchData()
Section titled “fetchData() ”protected function fetchData(): arrayFetches the next batch of data.
getRows()
Section titled “getRows() ”protected function getRows(): arrayReads and collects rows for batch
Returns
array
Since
2.0.23