我在Yii框架中工作。在我的索引操作中,我按以下方式创建了数据提供者
$connection=Yii::app()->db;
$user_id = Yii::app()->user->id;
$sql = 'SELECT * FROM post
LEFT JOIN comment ON post.id = comment.post_id
AND comment.user_id =:user_id
LIMIT 0 , 30 ';
$command=$connection->createCommand($sql);
$command->bindParam(':user_id', $user_id,PDO::PARAM_STR);
$rawData = $command->execute();
$dataProvider=new CArrayDataProvider($rawData, array(
'id'=>'user',
'sort'=>array(
'defaultOrder' => 'post.created',
),
'pagination'=>array(
'pageSize'=>10,
),
));
然后我渲染索引视图
$this->render('index',array(
'dataProvider'=>$dataProvider,
'category_id'=>$category_id,
));
索引视图正在执行
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
导致错误
Invalid argument supplied for foreach()
C:\wamp\www\yii\framework\web\CArrayDataProvider.php(140)
* @param array $directions the sorting directions (field name => whether it is descending sort)
*/
136 foreach($directions as $name=>$descending)
137 {
138 $column=array();
139 $fields_array=preg_split('/\.+/',$name,-1,PREG_SPLIT_NO_EMPTY);
140 foreach($this->rawData as $index=>$data)
141 $column[$index]=$this->getSortingFieldValue($data, $fields_array);
142 $args[]=&$column;
143 $dummy[]=&$column;
144 unset($column);
145 $direction=$descending ? SORT_DESC : SORT_ASC;
146 $args[]=&$direction;
147 $dummy[]=&$direction;
148 unset($direction);
149 }
你能帮帮我吗?
提前谢谢
答案 0 :(得分:0)
如果你var_dump($rawData)
,你会看到它是一个整数(或0)并且只返回结果数。
这是因为您在使用时使用$command->execute()
:
$command->queryAll();
这将返回一个结果数组,您可以将其传递到CActiveDataProvider