Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Feed Cgridview From Carraydataprovider

$
0
0
Hi. I have a city model which contains two attributes: id & city. I try to use CGridView and CArrayDataProvider instead of CActiveDataProvider. Here is my code for search function on city model:

	public function search()
	{
		$page = 1;
		
		if (isset($_GET[strtolower(__CLASS__) . '_page']))
			$page = intval($_GET[strtolower(__CLASS__) . '_page']);
		
		$offset = ($page - 1) * Yii::app()->params['pageSize'];
		$rowCount = Yii::app()->params['pageSize'];
		
		$sql = "SELECT t1.id, t1.city
				FROM tbl_country_city t1
				ORDER BY t1.city LIMIT {$offset}, {$rowCount}";
		
		$sqlCount = "SELECT COUNT(t1.id)
				FROM tbl_country_city t1
				ORDER BY t1.city";
				
		$countData = Yii::app()->db->createCommand($sqlCount)->queryScalar();
		$rawData = Yii::app()->db->createCommand($sql)->queryAll();
				
		$dataProvider = new CArrayDataProvider($rawData, array(
			'totalItemCount' => $countData,
		));
		
		//var_dump($rawData); <--- $rawData always contain appropriate data whatever the page it is on.
		//var_dump($dataProvider->getData()); <--- except on page 1, getData() resulting empty arrays.
		//exit();
		
		return $dataProvider;
	}


The problem is CGridView always displays no data for page above 1.

Viewing all articles
Browse latest Browse all 18717

Trending Articles