2015-11-17 4 views
8

こんにちはみんなを動作していないが、私はコマンドを見れば、それはそれでlimit示し比べしかし、私はActiveDataProviderにクエリを渡すときには、すべてのレコードをフェッチLIMITはActiveDataProviderに、私は次のコードと<code>limit</code> doesntの仕事を使用しています

$data= User::find()->where(['category_id'=> 5])->orderBy(['rand()' => SORT_DESC])->limit(4); 

    $command = $data->createCommand(); 

    $data2 = $command->queryAll();// This works fine and fetch only 4 data 

    $dataProvider = new ActiveDataProvider([ 
     'query' => $data, 

    ]); // But this displays all data without limit 

私はここで何をしていますか????

/** 
* @inheritdoc 
*/ 
protected function prepareModels() 
{ 
    if (!$this->query instanceof QueryInterface) { 
     throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.'); 
    } 
    $query = clone $this->query; 
    if (($pagination = $this->getPagination()) !== false) { 
     $pagination->totalCount = $this->getTotalCount(); 
     $query->limit($pagination->getLimit())->offset($pagination->getOffset()); 
    } 
    if (($sort = $this->getSort()) !== false) { 
     $query->addOrderBy($sort->getOrders()); 
    } 

    return $query->all($this->db); 
} 

私たちは、この部分に興味を持っている:改ページがfalseでない場合は、あなたが見ることができるように

if (($pagination = $this->getPagination()) !== false) { 
    $pagination->totalCount = $this->getTotalCount(); 
    $query->limit($pagination->getLimit())->offset($pagination->getOffset()); 
} 

、限度が管理されている。ここ

答えて

19

yii\data\ActiveDataProviderでモデルを準備するときに起こることです自動的に。

あなただけ falseに改ページを設定することができ、その後、制限の手動設定が動作します

$dataProvider = new ActiveDataProvider([ 
    'query' => $query, 
    'pagination' => false, 
]); 
関連する問題