# DataProviderInterface

> DataProviderInterface is the interface that must be implemented by data provider classes.

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

| interface | `yii\data\DataProviderInterface` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/data/DataProviderInterface.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/data/DataProviderInterface.php#L22) |

Implemented by 1 type:

- [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md)

DataProviderInterface is the interface that must be implemented by data provider classes.

Data providers are components that sort and paginate data, and provide them to widgets
such as [`GridView`](/docs/api/yii/grid/gridview/index.md), [`ListView`](/docs/api/yii/widgets/listview/index.md).

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

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`getCount()`](#method-getCount) abstract | Returns the number of data models in the current page. | `DataProviderInterface` |
| [`getKeys()`](#method-getKeys) abstract | Returns the key values associated with the data models. | `DataProviderInterface` |
| [`getModels()`](#method-getModels) abstract | Returns the data models in the current page. | `DataProviderInterface` |
| [`getPagination()`](#method-getPagination) abstract |  | `DataProviderInterface` |
| [`getSort()`](#method-getSort) abstract |  | `DataProviderInterface` |
| [`getTotalCount()`](#method-getTotalCount) abstract | Returns the total number of data models. | `DataProviderInterface` |
| [`prepare()`](#method-prepare) abstract | Prepares the data models and keys. | `DataProviderInterface` |

## Method details

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

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

Returns the number of data models in the current page.

This is equivalent to `count($provider->getModels())`.
When [pagination](/docs/api/yii/data/dataproviderinterface/index.md#method-getPagination) is false, this is the same as [totalCount](/docs/api/yii/data/dataproviderinterface/index.md#method-getTotalCount).

**Returns**

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

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

```php
abstract 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/dataproviderinterface/index.md#method-getModels). Each data model in [models](/docs/api/yii/data/dataproviderinterface/index.md#method-getModels) is uniquely identified by the corresponding key value in this array.

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

```php
abstract 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
abstract public function getPagination(): yii\data\Pagination|false
```

**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
abstract public function getSort(): yii\data\Sort|false
```

**Returns**

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

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

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

Returns the total number of data models.

When [pagination](/docs/api/yii/data/dataproviderinterface/index.md#method-getPagination) is false, this is the same as [count](/docs/api/yii/data/dataproviderinterface/index.md#method-getCount).

**Returns**

`int`: Total number of possible data models.

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

```php
abstract 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/dataproviderinterface/index.md#method-getModels) and [`getKeys()`](/docs/api/yii/data/dataproviderinterface/index.md#method-getKeys).

This method will be implicitly called by [`getModels()`](/docs/api/yii/data/dataproviderinterface/index.md#method-getModels) and [`getKeys()`](/docs/api/yii/data/dataproviderinterface/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`