Yii2 Yii::$container设置类的setter为何无效呢?

WBOY
Release: 2016-06-06 20:15:09
Original
1240 people have browsed it

<code>if(isset($_COOKIE['gridShowRows'])){
    Yii::$container->set('yii\data\BaseDataProvider',[
        'pagination'=>[
            'pageSize'=>$_COOKIE['gridShowRows']
        ]
    ]);
}</code>
Copy after login
Copy after login

yii\data\BaseDataProvider有一个setPagination的方法。

<code>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.');
        }
    }</code>
Copy after login
Copy after login

设置后不起效果。

但是下面的设置却又效果:

<code>if(isset($_COOKIE['gridShowRows'])){
    Yii::$container->set('yii\data\Pagination',[
        'pageSize'=>$_COOKIE['gridShowRows']
    ]);
}</code>
Copy after login
Copy after login

yii\data\Pagination有一个setPageSize的方法:

<code>/**
     * @param integer $value the number of items per page.
     * @param boolean $validatePageSize whether to validate page size.
     */
    public function setPageSize($value, $validatePageSize = false)
    {
        if ($value === null) {
            $this->_pageSize = null;
        } else {
            $value = (int) $value;
            if ($validatePageSize && isset($this->pageSizeLimit[0], $this->pageSizeLimit[1]) && count($this->pageSizeLimit) === 2) {
                if ($value pageSizeLimit[0]) {
                    $value = $this->pageSizeLimit[0];
                } elseif ($value > $this->pageSizeLimit[1]) {
                    $value = $this->pageSizeLimit[1];
                }
            }
            $this->_pageSize = $value;
        }
    }</code>
Copy after login
Copy after login

回复内容:

<code>if(isset($_COOKIE['gridShowRows'])){
    Yii::$container->set('yii\data\BaseDataProvider',[
        'pagination'=>[
            'pageSize'=>$_COOKIE['gridShowRows']
        ]
    ]);
}</code>
Copy after login
Copy after login

yii\data\BaseDataProvider有一个setPagination的方法。

<code>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.');
        }
    }</code>
Copy after login
Copy after login

设置后不起效果。

但是下面的设置却又效果:

<code>if(isset($_COOKIE['gridShowRows'])){
    Yii::$container->set('yii\data\Pagination',[
        'pageSize'=>$_COOKIE['gridShowRows']
    ]);
}</code>
Copy after login
Copy after login

yii\data\Pagination有一个setPageSize的方法:

<code>/**
     * @param integer $value the number of items per page.
     * @param boolean $validatePageSize whether to validate page size.
     */
    public function setPageSize($value, $validatePageSize = false)
    {
        if ($value === null) {
            $this->_pageSize = null;
        } else {
            $value = (int) $value;
            if ($validatePageSize && isset($this->pageSizeLimit[0], $this->pageSizeLimit[1]) && count($this->pageSizeLimit) === 2) {
                if ($value pageSizeLimit[0]) {
                    $value = $this->pageSizeLimit[0];
                } elseif ($value > $this->pageSizeLimit[1]) {
                    $value = $this->pageSizeLimit[1];
                }
            }
            $this->_pageSize = $value;
        }
    }</code>
Copy after login
Copy after login

检查传入的数组中的参数名。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template