yii\data\ArrayDataProvider::prepareModels PHP Method

prepareModels() protected method

protected prepareModels ( )
    protected function prepareModels()
    {
        if (($models = $this->allModels) === null) {
            return [];
        }
        if (($sort = $this->getSort()) !== false) {
            $models = $this->sortModels($models, $sort);
        }
        if (($pagination = $this->getPagination()) !== false) {
            $pagination->totalCount = $this->getTotalCount();
            if ($pagination->getPageSize() > 0) {
                $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
            }
        }
        return $models;
    }

Usage Example

 /**
  * @inheritdoc
  */
 protected function prepareModels()
 {
     $pattern = $this->getPattern();
     if ($pattern == false) {
         return parent::prepareModels();
     }
     $origAll = $this->allModels;
     $attribute = $this->alphaAttribute;
     $this->allModels = array_filter($this->allModels, function ($v) use($attribute, $pattern) {
         $attrVal = $v->{$attribute};
         if (is_array($pattern)) {
             $pattern = '/^' . current($pattern) . '/';
             return preg_match($pattern, $attrVal) === 1;
         } else {
             return stripos($attrVal, $pattern) === 0;
         }
     });
     $r = parent::prepareModels();
     $this->allModels = $origAll;
     return $r;
 }