没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\data\SqlDataProvider

继承yii\data\SqlDataProvider » yii\data\BaseDataProvider » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\data\DataProviderInterface
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/data/SqlDataProvider.php

SqlDataProvider implements a data provider based on a plain SQL statement.

SqlDataProvider provides data in terms of arrays, each representing a row of query result.

Like other data providers, SqlDataProvider also supports sorting and pagination. It does so by modifying the given $sql statement with "ORDER BY" and "LIMIT" clauses. You may configure the $sort and $pagination properties to customize sorting and pagination behaviors.

SqlDataProvider may be used in the following way:

$count = Yii::$app->db->createCommand('
    SELECT COUNT(*) FROM user WHERE status=:status
', [':status' => 1])->queryScalar();

$dataProvider = new SqlDataProvider([
    'sql' => 'SELECT * FROM user WHERE status=:status',
    'params' => [':status' => 1],
    'totalCount' => $count,
    'sort' => [
        'attributes' => [
            'age',
            'name' => [
                'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
                'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Name',
            ],
        ],
    ],
    'pagination' => [
        'pageSize' => 20,
    ],
]);

// get the user records in the current page
$models = $dataProvider->getModels();

Note: if you want to use the pagination feature, you must configure the $totalCount property to be the total number of rows (without pagination). And if you want to use the sorting feature, you must configure the $sort property so that the provider knows which columns can be sorted.

For more details and usage information on SqlDataProvider, see the guide article on data providers.

公共属性

隐藏继承的属性

属性类型描述被定义在
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$count integer The number of data models in the current page. yii\data\BaseDataProvider
$db yii\db\Connection|array|string The DB connection object or the application component ID of the DB connection. yii\data\SqlDataProvider
$id string An ID that uniquely identifies the data provider among all data providers. yii\data\BaseDataProvider
$key string|callable The column that is used as the key of the data models. yii\data\SqlDataProvider
$keys array The list of key values corresponding to $models. yii\data\BaseDataProvider
$models array The list of data models in the current page. yii\data\BaseDataProvider
$pagination yii\data\Pagination|false The pagination object. yii\data\BaseDataProvider
$params array Parameters (name=>value) to be bound to the SQL statement. yii\data\SqlDataProvider
$sort yii\data\Sort|boolean The sorting object. yii\data\BaseDataProvider
$sql string The SQL statement to be used for fetching data rows. yii\data\SqlDataProvider
$totalCount integer Total number of possible data models. yii\data\BaseDataProvider

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getCount() Returns the number of data models in the current page. yii\data\BaseDataProvider
getKeys() Returns the key values associated with the data models. yii\data\BaseDataProvider
getModels() Returns the data models in the current page. yii\data\BaseDataProvider
getPagination() Returns the pagination object used by this data provider. yii\data\BaseDataProvider
getSort() Returns the sorting object used by this data provider. yii\data\BaseDataProvider
getTotalCount() Returns the total number of data models. yii\data\BaseDataProvider
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the DB connection component. yii\data\SqlDataProvider
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
prepare() Prepares the data models and keys. yii\data\BaseDataProvider
refresh() Refreshes the data provider. yii\data\BaseDataProvider
setKeys() Sets the key values associated with the data models. yii\data\BaseDataProvider
setModels() Sets the data models in the current page. yii\data\BaseDataProvider
setPagination() Sets the pagination for this data provider. yii\data\BaseDataProvider
setSort() Sets the sort definition for this data provider. yii\data\BaseDataProvider
setTotalCount() Sets the total number of data models. yii\data\BaseDataProvider
trigger() Triggers an event. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述被定义在
prepareKeys() Prepares the keys associated with the currently available data models. yii\data\SqlDataProvider
prepareModels() Prepares the data models that will be made available in the current page. yii\data\SqlDataProvider
prepareTotalCount() Returns a value indicating the total number of data models in this data provider. yii\data\SqlDataProvider

属性详情

$db 公共 属性

The DB connection object or the application component ID of the DB connection. Starting from version 2.0.2, this can also be a configuration array for creating the object.

$key 公共 属性

The column that is used as the key of the data models. This can be either a column name, or a callable that returns the key value of a given data model.

If this is not set, the keys of the $models array will be used.

public string|callable $key null
$params 公共 属性

Parameters (name=>value) to be bound to the SQL statement.

public array $params = []
$sql 公共 属性

The SQL statement to be used for fetching data rows.

public string $sql null

方法详情

init() 公共 方法

Initializes the DB connection component.

This method will initialize the $db property to make sure it refers to a valid DB connection.

public void init()
throws yii\base\InvalidConfigException

if $db is invalid.

prepareKeys() 受保护 方法

Prepares the keys associated with the currently available data models.

protected array prepareKeys($models)
$models array

The available data models

return array

The keys

prepareModels() 受保护 方法

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

protected array prepareModels()
return array

The available data models

prepareTotalCount() 受保护 方法

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

protected integer prepareTotalCount()
return integer

Total number of data models in this data provider.