类 yii\data\ArrayDataProvider
ArrayDataProvider 实现了一个基于数据数组的数据提供者。
在 $allModels 属性中包含所有可能被排序和/或分页的数据模型。ArrayDataProvider 会在排序和/或分页后提供数据。您可以配置 $sort 和 $pagination 属性来定制排序和分页行为。
在 $allModels 数组中的元素可以是对象(例如模型对象)或关联数组(例如 DAO 的查询结果)。确保将 $key 属性设置为唯一标识数据记录的字段的名称,或者如果您没有这样的字段,则设置为 false。
与 yii\data\ActiveDataProvider 相比,ArrayDataProvider 的效率可能较低,因为它需要准备好 $allModels。
ArrayDataProvider 可以用以下方式使用
$query = new Query;
$provider = new ArrayDataProvider([
    'allModels' => $query->from('post')->all(),
    'sort' => [
        'attributes' => ['id', 'username', 'email'],
    ],
    'pagination' => [
        'pageSize' => 10,
    ],
]);
// get the posts in the current page
$posts = $provider->getModels();
注意:如果您想使用排序功能,您必须配置 $sort 属性,以便提供者知道哪些列可以排序。
有关 ArrayDataProvider 的更多详细信息和使用方法,请参阅 关于数据提供者的指南文章。
公共属性
| 属性 | 类型 | 描述 | 定义于 | 
|---|---|---|---|
| $allModels | array | 未分页或排序的数据。 | yii\data\ArrayDataProvider | 
| $behaviors | yii\base\Behavior[] | 附加到此组件的行为列表。 | yii\base\Component | 
| $count | integer | 当前页面中的数据模型数量。 | yii\data\BaseDataProvider | 
| $id | string|null | 在所有数据提供者中唯一标识数据提供者的 ID。 | yii\data\BaseDataProvider | 
| $key | string|callable|null | 用作数据模型键的列。 | yii\data\ArrayDataProvider | 
| $keys | array | 与 $models 对应的键值列表。 | yii\data\BaseDataProvider | 
| $modelClass | string | 将要表示的 Model 类名称。 | yii\data\ArrayDataProvider | 
| $models | array | 当前页面中的数据模型列表。 | yii\data\BaseDataProvider | 
| $pagination | yii\data\Pagination|false | 分页对象。 | yii\data\BaseDataProvider | 
| $sort | yii\data\Sort|boolean | 排序对象。 | yii\data\BaseDataProvider | 
| $totalCount | integer | 所有可能的数据模型总数。 | yii\data\BaseDataProvider | 
公共方法
受保护的方法
| 方法 | 描述 | 定义于 | 
|---|---|---|
| prepareKeys() | 准备与当前可用数据模型关联的键。 | yii\data\ArrayDataProvider | 
| prepareModels() | 准备将在当前页面中提供的数据模型。 | yii\data\ArrayDataProvider | 
| prepareTotalCount() | 返回一个值,指示此数据提供者中的数据模型总数。 | yii\data\ArrayDataProvider | 
| sortModels() | 根据给定的排序定义对数据模型进行排序。 | yii\data\ArrayDataProvider | 
属性详细信息
未分页或排序的数据。当启用分页时,此属性通常包含比 $models 更多的元素。数组元素必须使用以零为基的整数键。
方法详情
定义于: yii\base\Component::__call()
调用不是类方法的命名方法。
此方法将检查是否有任何附加的行为具有指定的方法,如果可用则执行该方法。
不要直接调用此方法,因为它是一个 PHP 魔术方法,将在调用未知方法时隐式调用。
| public mixed __call ( $name, $params ) | ||
| $name | string | 方法名称 | 
| $params | array | 方法参数 | 
| return | mixed | 方法返回值 | 
|---|---|---|
| throws | yii\base\UnknownMethodException | 调用未知方法时 | 
                public function __call($name, $params)
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $object) {
        if ($object->hasMethod($name)) {
            return call_user_func_array([$object, $name], $params);
        }
    }
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
            
        | public void __clone ( ) | 
                public function __clone()
{
    $this->_events = [];
    $this->_eventWildcards = [];
    $this->_behaviors = null;
}
            
        定义于: yii\base\BaseObject::__construct()
构造函数。
默认实现做了两件事
- 使用给定的配置 $config初始化对象。
- 调用 init()。
如果此方法在子类中被重写,建议
- 构造函数的最后一个参数是一个配置数组,就像这里的 $config。
- 在构造函数的末尾调用父级实现。
| public void __construct ( $config = [] ) | ||
| $config | array | 将用于初始化对象属性的名称-值对 | 
                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}
            
        定义于: yii\base\Component::__get()
返回组件属性的值。
此方法将按以下顺序检查并相应地采取行动
- 由 getter 定义的属性:返回 getter 的结果
- 行为的属性:返回行为属性的值
不要直接调用此方法,因为它是一个 PHP 魔术方法,将在执行 $value = $component->property; 时隐式调用。
另请参阅 __set()。
| public mixed __get ( $name ) | ||
| $name | string | 属性名称 | 
| return | mixed | 属性值或行为属性的值 | 
|---|---|---|
| throws | yii\base\UnknownPropertyException | 如果属性未定义 | 
| throws | yii\base\InvalidCallException | 如果属性是只写的。 | 
                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        // read property, e.g. getName()
        return $this->$getter();
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return $behavior->$name;
        }
    }
    if (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
            
        定义于: yii\base\Component::__isset()
检查属性是否已设置,即已定义且不为空。
此方法将按以下顺序检查并相应地采取行动
- 由 setter 定义的属性:返回属性是否已设置
- 行为的属性:返回属性是否已设置
- 对于不存在的属性,返回 false
不要直接调用此方法,因为它是一个 PHP 魔术方法,将在执行 isset($component->property) 时隐式调用。
| public boolean __isset ( $name ) | ||
| $name | string | 属性名称或事件名称 | 
| return | boolean | 指定的属性是否已设置 | 
|---|---|---|
                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return $behavior->$name !== null;
        }
    }
    return false;
}
            
        定义于: yii\base\Component::__set()
设置组件属性的值。
此方法将按以下顺序检查并相应地采取行动
- 由 setter 定义的属性:设置属性值
- 格式为 "on xyz" 的事件:将处理程序附加到事件 "xyz"
- 格式为 "as xyz" 的行为:附加名为 "xyz" 的行为
- 行为的属性:设置行为属性的值
不要直接调用此方法,因为它是一个 PHP 魔术方法,将在执行 $component->property = $value; 时隐式调用。
另请参阅 __get()。
| public void __set ( $name, $value ) | ||
| $name | string | 属性名称或事件名称 | 
| $value | mixed | 属性值 | 
| throws | yii\base\UnknownPropertyException | 如果属性未定义 | 
|---|---|---|
| throws | yii\base\InvalidCallException | 如果属性是只读的。 | 
                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        // set property
        $this->$setter($value);
        return;
    } elseif (strncmp($name, 'on ', 3) === 0) {
        // on event: attach event handler
        $this->on(trim(substr($name, 3)), $value);
        return;
    } elseif (strncmp($name, 'as ', 3) === 0) {
        // as behavior: attach behavior
        $name = trim(substr($name, 3));
        $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = $value;
            return;
        }
    }
    if (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
            
        定义于: yii\base\Component::__unset()
将组件属性设置为 null。
此方法将按以下顺序检查并相应地采取行动
- 由 setter 定义的属性:将属性值设置为 null
- 行为的属性:将属性值设置为 null
不要直接调用此方法,因为它是一个 PHP 魔术方法,将在执行 unset($component->property) 时隐式调用。
| public void __unset ( $name ) | ||
| $name | string | 属性名称 | 
| throws | yii\base\InvalidCallException | 如果属性是只读的。 | 
|---|---|---|
                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = null;
            return;
        }
    }
    throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}
            
        定义于: yii\base\Component::attachBehavior()
将行为附加到此组件。
此方法将根据给定的配置创建行为对象。之后,将通过调用 yii\base\Behavior::attach() 方法将行为对象附加到此组件。
另请参见 detachBehavior()。
| public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
| $name | string | 行为的名称。 | 
| $behavior | string|array|yii\base\Behavior | 行为配置。可以是以下之一 
 | 
| return | yii\base\Behavior | 行为对象 | 
|---|---|---|
                public function attachBehavior($name, $behavior)
{
    $this->ensureBehaviors();
    return $this->attachBehaviorInternal($name, $behavior);
}
            
        定义于: yii\base\Component::attachBehaviors()
将行为列表附加到组件。
每个行为都由其名称索引,并且应该是一个 yii\base\Behavior 对象、指定行为类的字符串或用于创建行为的配置数组。
另请参见 attachBehavior()。
| public void attachBehaviors ( $behaviors ) | ||
| $behaviors | array | 要附加到组件的行为列表 | 
                public function attachBehaviors($behaviors)
{
    $this->ensureBehaviors();
    foreach ($behaviors as $name => $behavior) {
        $this->attachBehaviorInternal($name, $behavior);
    }
}
            
        定义于: yii\base\Component::behaviors()
返回此组件应表现出的行为列表。
子类可以覆盖此方法来指定他们想要表现出的行为。
此方法的返回值应该是一个行为对象或配置的数组,这些对象或配置由行为名称索引。行为配置可以是指定行为类的字符串,也可以是以下结构的数组
'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]
请注意,行为类必须扩展自 yii\base\Behavior。行为可以使用名称或匿名附加。当使用名称作为数组键时,使用此名称,行为随后可以使用 getBehavior() 检索,也可以使用 detachBehavior() 分离。匿名行为无法检索或分离。
在此方法中声明的行为将自动附加到组件(按需)。
| public array behaviors ( ) | ||
| return | array | 行为配置。 | 
|---|---|---|
                public function behaviors()
{
    return [];
}
            
        定义于: yii\base\Component::canGetProperty()
返回一个值,指示属性是否可以读取。
如果属性可以读取,则
- 该类具有与指定名称关联的 getter 方法(在这种情况下,属性名称不区分大小写);
- 该类具有与指定名称相匹配的成员变量(当 $checkVars为 true 时);
- 已附加的行为具有给定名称的可读属性(当 $checkBehaviors为 true 时)。
另请参见 canSetProperty()。
| public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
| $name | string | 属性名称 | 
| $checkVars | boolean | 是否将成员变量视为属性 | 
| $checkBehaviors | boolean | 是否将行为的属性视为此组件的属性 | 
| return | boolean | 属性是否可读 | 
|---|---|---|
                public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canGetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}
            
        定义于: yii\base\Component::canSetProperty()
返回一个值,指示属性是否可以设置。
如果属性可以写入,则
- 该类具有与指定名称关联的 setter 方法(在这种情况下,属性名称不区分大小写);
- 该类具有与指定名称相匹配的成员变量(当 $checkVars为 true 时);
- 已附加的行为具有给定名称的可写属性(当 $checkBehaviors为 true 时)。
另请参见 canGetProperty()。
| public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
| $name | string | 属性名称 | 
| $checkVars | boolean | 是否将成员变量视为属性 | 
| $checkBehaviors | boolean | 是否将行为的属性视为此组件的属性 | 
| return | boolean | 属性是否可写 | 
|---|---|---|
                public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canSetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}
            
        ::class 代替。
                定义于: yii\base\BaseObject::className()
返回此类的完全限定名称。
| public static string className ( ) | ||
| return | string | 此类的完全限定名称。 | 
|---|---|---|
                public static function className()
{
    return get_called_class();
}
            
        | public yii\base\Behavior|null detachBehavior ( $name ) | ||
| $name | string | 行为的名称。 | 
| return | yii\base\Behavior|null | 分离的行为。如果行为不存在,则为 Null。 | 
|---|---|---|
                public function detachBehavior($name)
{
    $this->ensureBehaviors();
    if (isset($this->_behaviors[$name])) {
        $behavior = $this->_behaviors[$name];
        unset($this->_behaviors[$name]);
        $behavior->detach();
        return $behavior;
    }
    return null;
}
            
        定义于: yii\base\Component::detachBehaviors()
从组件中分离所有行为。
| public void detachBehaviors ( ) | 
                public function detachBehaviors()
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $name => $behavior) {
        $this->detachBehavior($name);
    }
}
            
        定义于: yii\base\Component::ensureBehaviors()
确保 behaviors() 中声明的行为附加到此组件。
| public void ensureBehaviors ( ) | 
                public function ensureBehaviors()
{
    if ($this->_behaviors === null) {
        $this->_behaviors = [];
        foreach ($this->behaviors() as $name => $behavior) {
            $this->attachBehaviorInternal($name, $behavior);
        }
    }
}
            
        定义于: yii\base\Component::getBehavior()
返回名为行为的对象。
| public yii\base\Behavior|null getBehavior ( $name ) | ||
| $name | string | 行为名称 | 
| return | yii\base\Behavior|null | 行为对象,如果行为不存在,则为 null | 
|---|---|---|
                public function getBehavior($name)
{
    $this->ensureBehaviors();
    return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
            
        定义于: yii\base\Component::getBehaviors()
返回附加到此组件的所有行为。
| public yii\base\Behavior[] getBehaviors ( ) | ||
| return | yii\base\Behavior[] | 附加到此组件的行为列表 | 
|---|---|---|
                public function getBehaviors()
{
    $this->ensureBehaviors();
    return $this->_behaviors;
}
            
        定义于: yii\data\BaseDataProvider::getCount()
返回当前页面中的数据模型数量。
| public integer getCount ( ) | ||
| return | integer | 当前页面中的数据模型数量。 | 
|---|---|---|
                public function getCount()
{
    return count($this->getModels());
}
            
        定义于: yii\data\BaseDataProvider::getKeys()
返回与数据模型关联的键值。
| public array getKeys ( ) | ||
| return | array | |
|---|---|---|
                public function getKeys()
{
    $this->prepare();
    return $this->_keys;
}
            
        定义于: yii\data\BaseDataProvider::getModels()
返回当前页面中的数据模型。
| public array getModels ( ) | ||
| return | array | 当前页面中的数据模型列表。 | 
|---|---|---|
                public function getModels()
{
    $this->prepare();
    return $this->_models;
}
            
        定义于: yii\data\BaseDataProvider::getPagination()
返回此数据提供者使用的分页对象。
请注意,您应该先调用 prepare() 或 getModels() 以获得 yii\data\Pagination::$totalCount 和 yii\data\Pagination::$pageCount 的正确值。
| public yii\data\Pagination|false getPagination ( ) | ||
| return | yii\data\Pagination|false | 分页对象。如果为 false,则表示分页已禁用。 | 
|---|---|---|
                public function getPagination()
{
    if ($this->_pagination === null) {
        $this->setPagination([]);
    }
    return $this->_pagination;
}
            
        定义于: yii\data\BaseDataProvider::getSort()
返回此数据提供者使用的排序对象。
| public yii\data\Sort|boolean getSort ( ) | ||
| return | yii\data\Sort|boolean | 排序对象。如果为 false,则表示排序已禁用。 | 
|---|---|---|
                public function getSort()
{
    if ($this->_sort === null) {
        $this->setSort([]);
    }
    return $this->_sort;
}
            
        定义于: yii\data\BaseDataProvider::getTotalCount()
返回数据模型的总数。
当 $pagination 为 false 时,此方法返回与 $count 相同的值。否则,它将调用 prepareTotalCount() 来获取计数。
| public integer getTotalCount ( ) | ||
| return | integer | 所有可能的数据模型总数。 | 
|---|---|---|
                public function getTotalCount()
{
    if ($this->getPagination() === false) {
        return $this->getCount();
    } elseif ($this->_totalCount === null) {
        $this->_totalCount = $this->prepareTotalCount();
    }
    return $this->_totalCount;
}
            
        定义于: yii\base\Component::hasEventHandlers()
返回一个值,指示是否为命名事件附加了任何处理程序。
| public boolean hasEventHandlers ( $name ) | ||
| $name | string | 事件名称 | 
| return | boolean | 是否存在任何附加到该事件的处理程序。 | 
|---|---|---|
                public function hasEventHandlers($name)
{
    $this->ensureBehaviors();
    if (!empty($this->_events[$name])) {
        return true;
    }
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
            return true;
        }
    }
    return Event::hasHandlers($this, $name);
}
            
        定义于: yii\base\Component::hasMethod()
返回一个值,指示是否定义了方法。
如果定义了方法
- 该类具有指定名称的方法
- 附加的行为具有给定名称的方法(当 $checkBehaviors为 true 时)。
| public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
| $name | string | 属性名称 | 
| $checkBehaviors | boolean | 是否将行为的方法视为此组件的方法 | 
| return | boolean | 该方法是否已定义 | 
|---|---|---|
                public function hasMethod($name, $checkBehaviors = true)
{
    if (method_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->hasMethod($name)) {
                return true;
            }
        }
    }
    return false;
}
            
        定义于: yii\base\Component::hasProperty()
返回一个值,指示是否为此组件定义了属性。
如果定义了属性
- 该类具有与指定名称关联的 getter 或 setter 方法(在这种情况下,属性名称不区分大小写);
- 该类具有与指定名称相匹配的成员变量(当 $checkVars为 true 时);
- 附加的行为具有给定名称的属性(当 $checkBehaviors为 true 时)。
另请参见
| public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
| $name | string | 属性名称 | 
| $checkVars | boolean | 是否将成员变量视为属性 | 
| $checkBehaviors | boolean | 是否将行为的属性视为此组件的属性 | 
| return | boolean | 该属性是否已定义 | 
|---|---|---|
                public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
    return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}
            
        | public void init ( ) | 
                public function init()
{
    parent::init();
    if ($this->id === null) {
        if (self::$counter > 0) {
            $this->id = 'dp-' . self::$counter;
        }
        self::$counter++;
    }
}
            
        定义于: yii\base\Component::off()
从此组件中分离现有的事件处理程序。
此方法与 on() 相反。
注意:如果事件名称传递了通配符模式,则只会删除使用此通配符注册的处理程序,而使用与该通配符匹配的普通名称注册的处理程序将保留。
另请参见 on().
| public boolean off ( $name, $handler = null ) | ||
| $name | string | 事件名称 | 
| $handler | callable|null | 要删除的事件处理程序。如果为 null,则将删除附加到指定事件的所有处理程序。 | 
| return | boolean | 如果找到并分离了处理程序 | 
|---|---|---|
                public function off($name, $handler = null)
{
    $this->ensureBehaviors();
    if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
        return false;
    }
    if ($handler === null) {
        unset($this->_events[$name], $this->_eventWildcards[$name]);
        return true;
    }
    $removed = false;
    // plain event names
    if (isset($this->_events[$name])) {
        foreach ($this->_events[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_events[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_events[$name] = array_values($this->_events[$name]);
            return true;
        }
    }
    // wildcard event names
    if (isset($this->_eventWildcards[$name])) {
        foreach ($this->_eventWildcards[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_eventWildcards[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
            // remove empty wildcards to save future redundant regex checks:
            if (empty($this->_eventWildcards[$name])) {
                unset($this->_eventWildcards[$name]);
            }
        }
    }
    return $removed;
}
            
        将事件处理程序附加到事件。
事件处理程序必须是一个有效的 PHP 回调。以下是一些示例
function ($event) { ... }         // anonymous function
[$object, 'handleClick']          // $object->handleClick()
['Page', 'handleClick']           // Page::handleClick()
'handleClick'                     // global function handleClick()
事件处理程序必须使用以下签名定义:
function ($event)
其中 $event 是一个 yii\base\Event 对象,包含与事件关联的参数。
从 2.0.14 版本开始,你可以使用通配符模式指定事件名称
$component->on('event.group.*', function ($event) {
    Yii::trace($event->name . ' is triggered.');
});
另请参见 off()。
| public void on ( $name, $handler, $data = null, $append = true ) | ||
| $name | string | 事件名称 | 
| $handler | callable | 事件处理程序 | 
| $data | mixed | 触发事件时传递给事件处理程序的数据。当事件处理程序被调用时,可以通过 yii\base\Event::$data 访问此数据。 | 
| $append | boolean | 是否将新的事件处理程序追加到现有处理程序列表的末尾。如果为 false,则新的处理程序将被插入到现有处理程序列表的开头。 | 
                public function on($name, $handler, $data = null, $append = true)
{
    $this->ensureBehaviors();
    if (strpos($name, '*') !== false) {
        if ($append || empty($this->_eventWildcards[$name])) {
            $this->_eventWildcards[$name][] = [$handler, $data];
        } else {
            array_unshift($this->_eventWildcards[$name], [$handler, $data]);
        }
        return;
    }
    if ($append || empty($this->_events[$name])) {
        $this->_events[$name][] = [$handler, $data];
    } else {
        array_unshift($this->_events[$name], [$handler, $data]);
    }
}
            
        定义于: yii\data\BaseDataProvider::prepare()
准备数据模型和键。
此方法将准备可以通过 getModels() 和 getKeys() 检索的数据模型和键。
如果之前尚未调用此方法,则 getModels() 和 getKeys() 将隐式调用此方法。
| public void prepare ( $forcePrepare = false ) | ||
| $forcePrepare | boolean | 是否强制数据准备,即使之前已经完成。 | 
                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);
    }
}
            
        准备与当前可用数据模型关联的键。
| protected array prepareKeys ( $models ) | ||
| $models | array | 可用的数据模型 | 
| return | array | 键 | 
|---|---|---|
                protected function prepareKeys($models)
{
    if ($this->key !== null) {
        $keys = [];
        foreach ($models as $model) {
            if (is_string($this->key)) {
                $keys[] = $model[$this->key];
            } else {
                $keys[] = call_user_func($this->key, $model);
            }
        }
        return $keys;
    }
    return array_keys($models);
}
            
        准备将在当前页面中提供的数据模型。
| protected array prepareModels ( ) | ||
| return | array | 可用的数据模型 | 
|---|---|---|
                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;
}
            
        返回一个值,指示此数据提供者中的数据模型总数。
| protected integer prepareTotalCount ( ) | ||
| return | integer | 此数据提供程序中的数据模型总数。 | 
|---|---|---|
                protected function prepareTotalCount()
{
    return is_array($this->allModels) ? count($this->allModels) : 0;
}
            
        定义于: yii\data\BaseDataProvider::refresh()
刷新数据提供者。
调用此方法后,如果再次调用 getModels()、getKeys() 或 getTotalCount(),它们将重新执行查询并返回最新的可用数据。
| public void refresh ( ) | 
                public function refresh()
{
    $this->_totalCount = null;
    $this->_models = null;
    $this->_keys = null;
}
            
        定义于: yii\data\BaseDataProvider::setKeys()
设置与数据模型关联的键值。
| public void setKeys ( $keys ) | ||
| $keys | array | 与 $models 对应的键值列表。 | 
                public function setKeys($keys)
{
    $this->_keys = $keys;
}
            
        定义于: yii\data\BaseDataProvider::setModels()
设置当前页面中的数据模型。
| public void setModels ( $models ) | ||
| $models | array | 当前页面中的模型 | 
                public function setModels($models)
{
    $this->_models = $models;
}
            
        定义于: yii\data\BaseDataProvider::setPagination()
为此数据提供者设置分页。
| public void setPagination ( $value ) | ||
| $value | array|yii\data\Pagination|boolean | 此数据提供程序要使用的分页。可以是以下之一 
 | 
| throws | yii\base\InvalidArgumentException | |
|---|---|---|
                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 InvalidArgumentException('Only Pagination instance, configuration array or false is allowed.');
    }
}
            
        定义于: yii\data\BaseDataProvider::setSort()
为此数据提供者设置排序定义。
| public void setSort ( $value ) | ||
| $value | array|yii\data\Sort|boolean | 此数据提供程序要使用的排序定义。可以是以下之一 
 | 
| throws | yii\base\InvalidArgumentException | |
|---|---|---|
                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 InvalidArgumentException('Only Sort instance, configuration array or false is allowed.');
    }
}
            
        定义于: yii\data\BaseDataProvider::setTotalCount()
设置数据模型的总数。
| public void setTotalCount ( $value ) | ||
| $value | integer | 数据模型的总数。 | 
                public function setTotalCount($value)
{
    $this->_totalCount = $value;
}
            
        根据给定的排序定义对数据模型进行排序。
| protected array sortModels ( $models, $sort ) | ||
| $models | array | 要排序的模型 | 
| $sort | yii\data\Sort | 排序定义 | 
| return | array | 排序后的数据模型 | 
|---|---|---|
                protected function sortModels($models, $sort)
{
    $orders = $sort->getOrders();
    if (!empty($orders)) {
        ArrayHelper::multisort($models, array_keys($orders), array_values($orders), $sort->sortFlags);
    }
    return $models;
}
            
        | public void trigger ( $name, yii\base\Event $event = null ) | ||
| $name | string | 事件名称 | 
| $event | yii\base\Event|null | 事件实例。如果未设置,将创建一个默认的 yii\base\Event 对象。 | 
                public function trigger($name, Event $event = null)
{
    $this->ensureBehaviors();
    $eventHandlers = [];
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (StringHelper::matchWildcard($wildcard, $name)) {
            $eventHandlers[] = $handlers;
        }
    }
    if (!empty($this->_events[$name])) {
        $eventHandlers[] = $this->_events[$name];
    }
    if (!empty($eventHandlers)) {
        $eventHandlers = call_user_func_array('array_merge', $eventHandlers);
        if ($event === null) {
            $event = new Event();
        }
        if ($event->sender === null) {
            $event->sender = $this;
        }
        $event->handled = false;
        $event->name = $name;
        foreach ($eventHandlers as $handler) {
            $event->data = $handler[1];
            call_user_func($handler[0], $event);
            // stop further handling if the event is handled
            if ($event->handled) {
                return;
            }
        }
    }
    // invoke class-level attached handlers
    Event::trigger($this, $name, $event);
}
            
        
注册 或 登录 以进行评论。