We talked about ActiveDataProvider, SqlDataProvider, and ArrayDataProvider yesterday. It’s not hard to see from the description that these products all behave similarly, so the developers of Yii2 set up a BaseDataProvider parent class for them. BaseDataProvider implements an interface called DataProviderInterface.

In PHP, the class that implements the interface must do all the functions declared in the interface. When we look at what methods are available in BaseDataProvider, we first look at the DataProviderInterface interface.

DataProviderInterface

  • prepare
  • getCount
  • getTotalCount
  • getModels
  • getKeys
  • getSort
  • getPagination

The above is the function declaration provided by DataProviderInterface for us, that is to say, BaseDataProvider class implements these functions, and our three brothers of DataProvider also inherit BaseDataProvider naturally have these methods.

So in yesterday’s article “On three data providers in YII2 and using them with GridView”, we boldly used getCount, getTotalCount, getModels, etc.

Let’s start with the method from the DataProviderInterface in the BaseDataProvider.

prepare

Data preparation, a functional function that assembles the _models and _keys properties in the DataProvider, is looked at in general.

public function prepare($forcePrepare = false){
    if ($forcePrepare || $this->_models === null) {
        $this->_models = $this->prepareModels();
    }
    if ($forcePrepare || $this->_keys === null) {
        $this->_keys = $this->prepareKeys($this->_models); }}Copy the code

To understand this function, we first need to look at the _models and _keys properties.

  • _Models This is easy to understand, a collection of objects or arrays that we get from getModels or GridView. Each term represents a specific piece of data.
  • _keys represents the unique key of each data item, which is the primary key of each data item when we use ActiveDataProvider, while the other two dataProviders are the key of the _Models array.

The Prepare function is defined only in the BaseDataProvider class, and each DataProvider defines its own prepareModels and prepareKeys methods. So we call prepare on different dataProviders and get different values.

$forcePrepare = $forcePrepare = $forcePrepare = $forcePrepare = $forcePrepare = $forcePrepare = $forcePrepare = $forcePrepare

getKeys

In preparing, we saw prepareModels and prepareKeys, and because of them, we can get the data properly through getModels and getKeys.

Let’s start with the getKeys function, which is essentially the contents of the $_keys property in the DataProvider.

Here’s a little bit different. Let’s look at it.

ActiveDataProvider

$dataProvider = new ActiveDataProvider([
    'query' => Blog::find()->select(['title'.'id']),
    'pagination'= > ['pageSize'= >20,]]); VarDumper::dump($dataProvider->getKeys(),10.true);
Copy the code

Here’s what we get

[
    0= >1
    1= >2
    2= >3
    3= >4
    4= >5
    5= >9
]
Copy the code

Yes, you must have noticed that _keys is an array where the key represents each row of data items and the value represents the primary key of the corresponding data items.

Note: Null is returned if there is no ID column in your data item. For example, $query is Blog::find()->select([‘title’]).

For ArrayDataProvider and SqlDataProvider providers, getKeys gets a much simpler array, and the data item is an array, so it looks like this

[
    0= >0
    1= >1
    2= >2
    3= >3
    4= >4
    5= >5
]
Copy the code

You can also override these rules by using the setKeys function, which will be covered in a later chapter

getCount & getTotalCount

These are two functions about quantity statistics, and the intent is obvious, because the DataProvider supports paging.

  • GetCount Specifies the number of data items on the current page
  • GetTotalCount Total number of data items

getSort

To get the sort information, this method is incredibly simple to implement. Let’s see.

public function getSort(a)
{
    if ($this->_sort === null) {
        $this->setSort([]);
    }

    return $this->_sort;
}
Copy the code

The _sort attribute is returned by the setSort method. If the value of _sort is returned by the setSort method, the value of _sort is returned by the setSort method

Let’s post the code first

public function setSort($value){
    if (is_array($value)) {
        $config = ['class' => Sort::className()];
        if ($this->id ! = =null) {
            $config['sortParam'] = $this->id . '-sort';
        }
        $this->_sort = Yii::createObject(array_merge($config, $value));
    } elseif ($value instanceof Sort || $value === false) {
        $this->_sort = $value;
    } else {
        throw new InvalidParamException('Only Sort instance, configuration array or false is allowed.'); }}Copy the code

There are three possibilities for passing $value

  • Array Indicates a configuration Array
  • Sort A Sort object
  • Bool Indicates the value of a Boolean type

These can be passed in when we create a DataProvider object, for example

$dataProvider = new ActiveDataProvider([
    'query' => Blog::find()->select(['title'.'id']),
    'sort'= >false
]);
Copy the code

If sort is false, it means that sort is not sorted. If sort is false, it means that sort is not sorted. If sort is false, it means that sort is not sorted.

The setSort method does different logic depending on the type of the argument. For example, passing an array type generates a Sort object and takes your setting as an argument to create a new object. You can also pass a Sort object directly to get the same effect.

getPagination

This is an important function that takes care of paging.

public function getPagination(a)
{
    if ($this->_pagination === null) {
        $this->setPagination([]);
    }

    return $this->_pagination;
}
Copy the code

But don’t worry, this method is exactly the same as getSort. When we find that _pagination of an object doesn’t exist, we call setPagination to set it, and you’ll be familiar with the code that follows.

public function setPagination($value)
{
    if (is_array($value)) {
        $config = ['class' => Pagination::className()];
        if ($this->id ! = =null) {
            $config['pageParam'] = $this->id . '-page';
            $config['pageSizeParam'] = $this->id . '-per-page';
        }
        $this->_pagination = Yii::createObject(array_merge($config, $value));
    } elseif ($value instanceof Pagination || $value === false) {
        $this->_pagination = $value;
    } else {
        throw new InvalidParamException('Only Pagination instance, configuration array or false is allowed.'); }}Copy the code

Right? You notice that setSort is exactly the same as setSort, receiving the same function.

summary

Now do you know how DataProvider works and how it is used? I’ll show you how DataProvider objects are handled in a GridView.

This article is from https://nai8.me