2016-12-05 4 views
0
<?php 
/** 
* @link http://www.yiiframework.com/ 
* @copyright Copyright (c) 2008 Yii Software LLC 
* @license http://www.yiiframework.com/license/ 
*/ 

namespace yii\data; 

use Yii; 
use yii\base\Component; 
use yii\base\InvalidParamException; 

/** 
* BaseDataProvider provides a base class that implements the [[DataProviderInterface]]. 
* 
* @property integer $count The number of data models in the current page. This property is read-only. 
* @property array $keys The list of key values corresponding to [[models]]. Each data model in [[models]] is 
* uniquely identified by the corresponding key value in this array. 
* @property array $models The list of data models in the current page. 
* @property Pagination|boolean $pagination The pagination object. If this is false, it means the pagination 
* is disabled. Note that the type of this property differs in getter and setter. See [[getPagination()]] and 
* [[setPagination()]] for details. 
* @property Sort|boolean $sort The sorting object. If this is false, it means the sorting is disabled. Note 
* that the type of this property differs in getter and setter. See [[getSort()]] and [[setSort()]] for details. 
* @property integer $totalCount Total number of possible data models. 
* 
* @author Qiang Xue <[email protected]> 
* @since 2.0 
*/ 
abstract class BaseDataProvider extends Component implements DataProviderInterface 
{ 
    /** 
    * @var string an ID that uniquely identifies the data provider among all data providers. 
    * You should set this property if the same page contains two or more different data providers. 
    * Otherwise, the [[pagination]] and [[sort]] may not work properly. 
    */ 
    public $id; 

    private $_sort; 
    private $_pagination; 
    private $_keys; 
    private $_models; 
    private $_totalCount; 


    /** 
    * Prepares the data models that will be made available in the current page. 
    * @return array the available data models 
    */ 
    abstract protected function prepareModels(); 

    /** 
    * Prepares the keys associated with the currently available data models. 
    * @param array $models the available data models 
    * @return array the keys 
    */ 
    abstract protected function prepareKeys($models); 

    /** 
    * Returns a value indicating the total number of data models in this data provider. 
    * @return integer total number of data models in this data provider. 
    */ 
    abstract protected function prepareTotalCount(); 

    /** 
    * Prepares the data models and keys. 
    * 
    * This method will prepare the data models and keys that can be retrieved via 
    * [[getModels()]] and [[getKeys()]]. 
    * 
    * This method will be implicitly called by [[getModels()]] and [[getKeys()]] if it has not been called before. 
    * 
    * @param boolean $forcePrepare whether to force data preparation even if it has been done before. 
    */ 
    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); 
     } 
    } 

    /** 
    * Returns the data models in the current page. 
    * @return array the list of data models in the current page. 
    */ 
    public function getModels() 
    { 
     $this->prepare(); 

     return $this->_models; 
    } 

    /** 
    * Sets the data models in the current page. 
    * @param array $models the models in the current page 
    */ 
    public function setModels($models) 
    { 
     $this->_models = $models; 
    } 

    /** 
    * Returns the key values associated with the data models. 
    * @return array the list of key values corresponding to [[models]]. Each data model in [[models]] 
    * is uniquely identified by the corresponding key value in this array. 
    */ 
    public function getKeys() 
    { 
     $this->prepare(); 

     return $this->_keys; 
    } 

    /** 
    * Sets the key values associated with the data models. 
    * @param array $keys the list of key values corresponding to [[models]]. 
    */ 
    public function setKeys($keys) 
    { 
     $this->_keys = $keys; 
    } 

    /** 
    * Returns the number of data models in the current page. 
    * @return integer the number of data models in the current page. 
    */ 
    public function getCount() 
    { 
     return count($this->getModels()); 
    } 

    /** 
    * Returns the total number of data models. 
    * When [[pagination]] is false, this returns the same value as [[count]]. 
    * Otherwise, it will call [[prepareTotalCount()]] to get the count. 
    * @return integer total number of possible data models. 
    */ 
    public function getTotalCount() 
    { 
     if ($this->getPagination() === false) { 
      return $this->getCount(); 
     } elseif ($this->_totalCount === null) { 
      $this->_totalCount = $this->prepareTotalCount(); 
     } 

     return $this->_totalCount; 
    } 

    /** 
    * Sets the total number of data models. 
    * @param integer $value the total number of data models. 
    */ 
    public function setTotalCount($value) 
    { 
     $this->_totalCount = $value; 
    } 

    /** 
    * Returns the pagination object used by this data provider. 
    * Note that you should call [[prepare()]] or [[getModels()]] first to get correct values 
    * of [[Pagination::totalCount]] and [[Pagination::pageCount]]. 
    * @return Pagination|boolean the pagination object. If this is false, it means the pagination is disabled. 
    */ 
    public function getPagination() 
    { 
     if ($this->_pagination === null) { 
      $this->setPagination([]); 
     } 

     return $this->_pagination; 
    } 

    /** 
    * Sets the pagination for this data provider. 
    * @param array|Pagination|boolean $value 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]] or its subclass 
    * - false, if pagination needs to be disabled. 
    * 
    * @throws InvalidParamException 
    */ 
    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.'); 
     } 
    } 

    /** 
    * Returns the sorting object used by this data provider. 
    * @return Sort|boolean the sorting object. If this is false, it means the sorting is disabled. 
    */ 
    public function getSort() 
    { 
     if ($this->_sort === null) { 
      $this->setSort([]); 
     } 

     return $this->_sort; 
    } 

    /** 
    * Sets the sort definition for this data provider. 
    * @param array|Sort|boolean $value 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]] or its subclass 
    * - false, if sorting needs to be disabled. 
    * 
    * @throws InvalidParamException 
    */ 
    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.'); 
     } 
    } 

    /** 
    * Refreshes the data provider. 
    * After calling this method, if [[getModels()]], [[getKeys()]] or [[getTotalCount()]] is called again, 
    * they will re-execute the query and return the latest data available. 
    */ 
    public function refresh() 
    { 
     $this->_totalCount = null; 
     $this->_models = null; 
     $this->_keys = null; 
    } 
} 

위의 코드는 yii2의 BaseDataProvider입니다. 내 질문은 어떻게 yii2 _models 및 _keys 설정할 수 있습니다? 어떤 파일을 링크해야합니까? 미안 나는 yii에 아주 새롭다. 가능한 경우 사례를 제공해주십시오. 감사합니다.BaseDataProvider에 _models 및 _keys를 설정하는 방법

답변

2

여기에 붙여 넣은 것은 추상 Yii2 클래스입니다.이 클래스는 절대로 편집하면 안됩니다.

여기 ActiveDataProvider에 대해 읽고 난 당신을 제안이 일을 사용하려면 : Docs

$query = Post::find()->where(['status' => 1]); 
$provider = new ActiveDataProvider([ 
    'query' => $query, 
]); 

여기 그것을 사용하는 방법을 예입니다, 첫 번째 줄 ActiveDataProvider를 채우는 데 사용되는 데이터를 정의 (그것은 SQL 쿼리입니다) , 그리고 config 매개 변수로 쿼리를 사용하여 ActiveDataProvider 인스턴스를 만듭니다.

관련 문제