# BaseDataProvider

> BaseDataProvider provides a base class that implements the yii\data\DataProviderInterface.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [data](/docs/api/yii/data/index.md) › BaseDataProvider

| abstract class | `yii\data\BaseDataProvider` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`DataProviderInterface`](/docs/api/yii/data/dataproviderinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/data/BaseDataProvider.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/data/BaseDataProvider.php#L46) |

3 subclasses:

- [`ActiveDataProvider`](/docs/api/yii/data/activedataprovider/index.md)
- [`ArrayDataProvider`](/docs/api/yii/data/arraydataprovider/index.md)
- [`SqlDataProvider`](/docs/api/yii/data/sqldataprovider/index.md)

BaseDataProvider provides a base class that implements the [`DataProviderInterface`](/docs/api/yii/data/dataproviderinterface/index.md).

For more details and usage information on BaseDataProvider, see the [guide article on data providers](/docs/guide/output-data-providers/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <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) |
| <span id="property-count"></span>`$count` read-only | `int` | The number of data models in the current page. | `BaseDataProvider` |
| [`$id`](#property-id) | `string\|null` | An ID that uniquely identifies the data provider among all data providers. Default: `null`. | `BaseDataProvider` |
| [`$keys`](#property-keys) | `array` | The list of key values corresponding to [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models). | `BaseDataProvider` |
| <span id="property-models"></span>`$models` | `array` | The list of data models in the current page. | `BaseDataProvider` |
| [`$pagination`](#property-pagination) | `array\|`[`Pagination`](/docs/api/yii/data/pagination/index.md)`\|false` | The pagination to be used by this data provider. | `BaseDataProvider` |
| [`$sort`](#property-sort) | `array\|`[`Sort`](/docs/api/yii/data/sort/index.md)`\|bool` | The sort definition to be used by this data provider. | `BaseDataProvider` |
| <span id="property-totalCount"></span>`$totalCount` | `int` | Total number of possible data models. | `BaseDataProvider` |

## 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) |
| <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) |
| <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) |
| <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) |
| [`getCount()`](#method-getCount) | Returns the number of data models in the current page. | `BaseDataProvider` |
| [`getKeys()`](#method-getKeys) | Returns the key values associated with the data models. | `BaseDataProvider` |
| [`getModels()`](#method-getModels) | Returns the data models in the current page. | `BaseDataProvider` |
| [`getPagination()`](#method-getPagination) | Returns the pagination object used by this data provider. | `BaseDataProvider` |
| [`getSort()`](#method-getSort) | Returns the sorting object used by this data provider. | `BaseDataProvider` |
| [`getTotalCount()`](#method-getTotalCount) | Returns the total number of data models. | `BaseDataProvider` |
| <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) |
| [`init()`](#method-init) | Initializes the object. | `BaseDataProvider` |
| <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) |
| [`prepare()`](#method-prepare) | Prepares the data models and keys. | `BaseDataProvider` |
| [`refresh()`](#method-refresh) | Refreshes the data provider. | `BaseDataProvider` |
| [`setKeys()`](#method-setKeys) | Sets the key values associated with the data models. | `BaseDataProvider` |
| [`setModels()`](#method-setModels) | Sets the data models in the current page. | `BaseDataProvider` |
| [`setPagination()`](#method-setPagination) | Sets the pagination for this data provider. | `BaseDataProvider` |
| [`setSort()`](#method-setSort) | Sets the sort definition for this data provider. | `BaseDataProvider` |
| [`setTotalCount()`](#method-setTotalCount) | Sets the total number of data models. | `BaseDataProvider` |
| <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) |
| [`prepareKeys()`](#method-prepareKeys) protected abstract | Prepares the keys associated with the currently available data models. | `BaseDataProvider` |
| [`prepareModels()`](#method-prepareModels) protected abstract | Prepares the data models that will be made available in the current page. | `BaseDataProvider` |
| [`prepareTotalCount()`](#method-prepareTotalCount) protected abstract | Returns a value indicating the total number of data models in this data provider. | `BaseDataProvider` |

## Property details

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

```php
public string|null $id = null
```

An ID that uniquely identifies the data provider among all data providers.

Generated automatically the following way in case it is not set:

- First data provider ID is empty.
- Second and all subsequent data provider IDs are: "dp-1", "dp-2", etc.

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

```php
public array $keys
```

The list of key values corresponding to [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models).

Each data model in [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models) is
uniquely identified by the corresponding key value in this array.

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

```php
public array|yii\data\Pagination|false $pagination
```

The pagination to be used by this data provider.

This can be one of the following:

- a configuration array for creating the pagination object. The "class" element defaults
  to 'yii\data\Pagination'
- an instance of [`Pagination`](/docs/api/yii/data/pagination/index.md) or its subclass
- false, if pagination needs to be disabled.

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

```php
public array|yii\data\Sort|bool $sort
```

The sort definition to be used by this data provider.

This can be one of the following:

- a configuration array for creating the sort definition object. The "class" element defaults
  to 'yii\data\Sort'
- an instance of [`Sort`](/docs/api/yii/data/sort/index.md) or its subclass
- false, if sorting needs to be disabled.

## Method details

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

```php
public function getCount(): int
```

Returns the number of data models in the current page.

**Returns**

`int`: The number of data models in the current page.

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

```php
public function getKeys(): array
```

Returns the key values associated with the data models.

**Returns**

`array`: The list of key values corresponding to [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models). Each data model in [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models) is uniquely identified by the corresponding key value in this array.

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

```php
public function getModels(): array
```

Returns the data models in the current page.

**Returns**

`array`: The list of data models in the current page.

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

```php
public function getPagination(): yii\data\Pagination|false
```

Returns the pagination object used by this data provider.

Note that you should call [`prepare()`](/docs/api/yii/data/basedataprovider/index.md#method-prepare) or [`getModels()`](/docs/api/yii/data/basedataprovider/index.md#method-getModels) first to get correct values
of [`Pagination::$totalCount`](/docs/api/yii/data/pagination/index.md#property-totalCount) and [`Pagination::$pageCount`](/docs/api/yii/data/pagination/index.md#property-pageCount).

**Returns**

[`Pagination`](/docs/api/yii/data/pagination/index.md)`|false`: The pagination object. If this is false, it means the pagination is disabled.

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

```php
public function getSort(): yii\data\Sort|bool
```

Returns the sorting object used by this data provider.

**Returns**

[`Sort`](/docs/api/yii/data/sort/index.md)`|bool`: The sorting object. If this is false, it means the sorting is disabled.

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

```php
public function getTotalCount(): int
```

Returns the total number of data models.

When [`$pagination`](/docs/api/yii/data/basedataprovider/index.md#property-pagination) is false, this returns the same value as [`$count`](/docs/api/yii/data/basedataprovider/index.md#property-count).
Otherwise, it will call [`prepareTotalCount()`](/docs/api/yii/data/basedataprovider/index.md#method-prepareTotalCount) to get the count.

**Returns**

`int`: Total number of possible data models.

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

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

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

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

```php
public function prepare(bool $forcePrepare = false): mixed
```

Prepares the data models and keys.

This method will prepare the data models and keys that can be retrieved via
[`getModels()`](/docs/api/yii/data/basedataprovider/index.md#method-getModels) and [`getKeys()`](/docs/api/yii/data/basedataprovider/index.md#method-getKeys).

This method will be implicitly called by [`getModels()`](/docs/api/yii/data/basedataprovider/index.md#method-getModels) and [`getKeys()`](/docs/api/yii/data/basedataprovider/index.md#method-getKeys) if it has not been called before.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$forcePrepare` | `bool` | Whether to force data preparation even if it has been done before. |

**Returns**

`mixed`

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

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

Refreshes the data provider.

After calling this method, if [`getModels()`](/docs/api/yii/data/basedataprovider/index.md#method-getModels), [`getKeys()`](/docs/api/yii/data/basedataprovider/index.md#method-getKeys) or [`getTotalCount()`](/docs/api/yii/data/basedataprovider/index.md#method-getTotalCount) is called again,
they will re-execute the query and return the latest data available.

**Returns**

`mixed`

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

```php
public function setKeys(array $keys): mixed
```

Sets the key values associated with the data models.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$keys` | `array` | The list of key values corresponding to [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models). |

**Returns**

`mixed`

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

```php
public function setModels(array $models): mixed
```

Sets the data models in the current page.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$models` | `array` | The models in the current page |

**Returns**

`mixed`

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

```php
public function setPagination(array|yii\data\Pagination|false $value): mixed
```

Sets the pagination for this data provider.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `array\|`[`Pagination`](/docs/api/yii/data/pagination/index.md)`\|false` | The pagination to be used by this data provider. This can be one of the following: - a configuration array for creating the pagination object. The "class" element defaults to 'yii\data\Pagination' - an instance of [`Pagination`](/docs/api/yii/data/pagination/index.md) or its subclass - false, if pagination needs to be disabled. |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md)

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

```php
public function setSort(array|yii\data\Sort|bool $value): mixed
```

Sets the sort definition for this data provider.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `array\|`[`Sort`](/docs/api/yii/data/sort/index.md)`\|bool` | The sort definition to be used by this data provider. This can be one of the following: - a configuration array for creating the sort definition object. The "class" element defaults to 'yii\data\Sort' - an instance of [`Sort`](/docs/api/yii/data/sort/index.md) or its subclass - false, if sorting needs to be disabled. |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md)

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

```php
public function setTotalCount(int $value): mixed
```

Sets the total number of data models.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `int` | The total number of data models. |

**Returns**

`mixed`

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

```php
abstract protected function prepareKeys(array $models): array
```

Prepares the keys associated with the currently available data models.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$models` | `array` | The available data models |

**Returns**

`array`: The keys

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

```php
abstract protected function prepareModels(): array
```

Prepares the data models that will be made available in the current page.

**Returns**

`array`: The available data models

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

```php
abstract protected function prepareTotalCount(): int
```

Returns a value indicating the total number of data models in this data provider.

**Returns**

`int`: Total number of data models in this data provider.