# BatchQueryResult

> BatchQueryResult represents a batch query from which you can retrieve data in batches.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › BatchQueryResult

| class | `yii\db\BatchQueryResult` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`Iterator`](https://www.php.net/manual/en/class.iterator.php), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/db/BatchQueryResult.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/BatchQueryResult.php#L36) |

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()`](/docs/api/yii/db/query/index.md#method-batch) or [`Query::each()`](/docs/api/yii/db/query/index.md#method-each). Because BatchQueryResult implements the [`Iterator`](https://www.php.net/manual/en/class.iterator.php) 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

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-batchSize"></span>`$batchSize` | `int` | The number of rows to be returned in each batch. Default: `100`. | `BatchQueryResult` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$db`](#property-db) | [`Connection`](/docs/api/yii/db/connection/index.md)`\|null` | The DB connection to be used when performing batch query. Default: `null`. | `BatchQueryResult` |
| [`$each`](#property-each) | `bool` | Whether to return a single row during each iteration. Default: `false`. | `BatchQueryResult` |
| [`$query`](#property-query) | [`Query`](/docs/api/yii/db/query/index.md) | The query object associated with this batch query. Default: `null`. | `BatchQueryResult` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`__destruct()`](#method-__destruct) | Destructor. | `BatchQueryResult` |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`__wakeup()`](#method-__wakeup) | Unserialization is disabled to prevent remote code execution in case application calls unserialize() on user input containing specially crafted string. | `BatchQueryResult` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`current()`](#method-current) | Returns the current dataset. | `BatchQueryResult` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`key()`](#method-key) | Returns the index of the current dataset. | `BatchQueryResult` |
| [`next()`](#method-next) | Moves the internal pointer to the next dataset. | `BatchQueryResult` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`reset()`](#method-reset) | Resets the batch query. | `BatchQueryResult` |
| [`rewind()`](#method-rewind) | Resets the iterator to the initial state. | `BatchQueryResult` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`valid()`](#method-valid) | Returns whether there is a valid dataset at the current position. | `BatchQueryResult` |
| [`fetchData()`](#method-fetchData) protected | Fetches the next batch of data. | `BatchQueryResult` |
| [`getRows()`](#method-getRows) protected | Reads and collects rows for batch | `BatchQueryResult` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_RESET"></span>`EVENT_RESET` | `yii\db\Event` | An event that is triggered when the batch query is reset. Event name: `'reset'`. Available since 2.0.41. | `BatchQueryResult` |
| <span id="event-EVENT_FINISH"></span>`EVENT_FINISH` | `yii\db\Event` | An event that is triggered when the last batch has been fetched. Event name: `'finish'`. Available since 2.0.41. | `BatchQueryResult` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-MSSQL_NO_MORE_ROWS_ERROR_CODE"></span>`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

### $db <span id="property-db"></span>

```php
public yii\db\Connection|null $db = null
```

The DB connection to be used when performing batch query.

If null, the "db" application component will be used.

### $each <span id="property-each"></span>

```php
public bool $each = false
```

Whether to return a single row during each iteration.

If false, a whole batch of rows will be returned in each iteration.

### $query <span id="property-query"></span>

```php
public yii\db\Query $query = null
```

The query object associated with this batch query.

Do not modify this property directly unless after [`reset()`](/docs/api/yii/db/batchqueryresult/index.md#method-reset) is called explicitly.

## Method details

### __destruct() <span id="method-__destruct"></span>

```php
public function __destruct(): mixed
```

Destructor.

**Returns**

`mixed`

### __wakeup() <span id="method-__wakeup"></span>

```php
public function __wakeup(): mixed
```

Unserialization is disabled to prevent remote code execution in case application
calls unserialize() on user input containing specially crafted string.

**Returns**

`mixed`

**Since**

2.0.38

**See also**

- <https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-15148>

### current() <span id="method-current"></span>

```php
public function current(): yii\db\TValue
```

Returns the current dataset.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`yii\db\TValue`: The current dataset.

### key() <span id="method-key"></span>

```php
public function key(): yii\db\TKey|null
```

Returns the index of the current dataset.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`yii\db\TKey|null`: The index of the current row.

### next() <span id="method-next"></span>

```php
public function next(): mixed
```

Moves the internal pointer to the next dataset.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`mixed`

### reset() <span id="method-reset"></span>

```php
public function reset(): mixed
```

Resets the batch query.

This method will clean up the existing batch query so that a new batch query can be performed.

**Returns**

`mixed`

### rewind() <span id="method-rewind"></span>

```php
public function rewind(): mixed
```

Resets the iterator to the initial state.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`mixed`

### valid() <span id="method-valid"></span>

```php
public function valid(): bool
```

Returns whether there is a valid dataset at the current position.

This method is required by the interface [`Iterator`](https://www.php.net/manual/en/class.iterator.php).

**Returns**

`bool`: Whether there is a valid dataset at the current position.

### fetchData() <span id="method-fetchData"></span>

```php
protected function fetchData(): array
```

Fetches the next batch of data.

**Returns**

`array`: The data fetched

**Throws**

- [`Exception`](/docs/api/yii/db/exception/index.md)

### getRows() <span id="method-getRows"></span>

```php
protected function getRows(): array
```

Reads and collects rows for batch

**Returns**

`array`

**Since**

2.0.23