我是Yii的新手。我正在使用CArrayDataProvider在我的CGridView中传递数据,但是我收到了一个未定义id的错误。你能帮我检查一下我是否错过了什么?
index.php(查看)
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $arrayDataProvider,
'columns' => array(
array(
'name' => 'LastName',
'type' => 'raw',
'value' => 'CHtml::encode($data["LastName"])'
),
array(
'name' => 'FirstName',
'type' => 'raw',
'value' => 'CHtml::link(CHtml::encode($data["FirstName"])',
),
),
));
RecordsController.php
public function actionIndex()
{
$data = new TblAccountdetails();
$rawData = $data->getAllData();
$arrayDataProvider=new CArrayDataProvider($rawData, array(
'id'=>'id',
'pagination'=>array(
'pageSize'=>10,
),
));
$params =array(
'arrayDataProvider'=>$arrayDataProvider,
);
if(!isset($_GET['ajax'])) $this->render('index', $params);
else $this->renderPartial('index', $params);
}
我的模型功能
public function getAllData()
{
$user = Yii::app()->db->createCommand()
->select('AID, LastName, FirstName')
->from('tbl_accountdetails')
->queryAll();
return $user;
}
$ arrayDataProvider的Vardump
object(CArrayDataProvider)#32 (11) {
["keyField"]=>
string(2) "id"
["rawData"]=>
array(3) {
[0]=>
array(3) {
["AID"]=>
string(1) "1"
["LastName"]=>
string(8) "Floresca"
["FirstName"]=>
string(5) "Joene"
}
[1]=>
array(3) {
["AID"]=>
string(1) "2"
["LastName"]=>
string(13) "aaaaaaaaaaaaa"
["FirstName"]=>
string(12) "aaaaaaaaaaaa"
}
[2]=>
array(3) {
["AID"]=>
string(1) "3"
["LastName"]=>
string(7) "Antonio"
["FirstName"]=>
string(4) "Noel"
}
}
["caseSensitiveSort"]=>
bool(true)
["_id":"CDataProvider":private]=>
string(2) "id"
["_data":"CDataProvider":private]=>
NULL
["_keys":"CDataProvider":private]=>
NULL
["_totalItemCount":"CDataProvider":private]=>
NULL
["_sort":"CDataProvider":private]=>
NULL
["_pagination":"CDataProvider":private]=>
object(CPagination)#33 (9) {
["pageVar"]=>
string(7) "id_page"
["route"]=>
string(0) ""
["params"]=>
NULL
["validateCurrentPage"]=>
bool(true)
["_pageSize":"CPagination":private]=>
int(10)
["_itemCount":"CPagination":private]=>
int(0)
["_currentPage":"CPagination":private]=>
NULL
["_e":"CComponent":private]=>
NULL
["_m":"CComponent":private]=>
NULL
}
["_e":"CComponent":private]=>
NULL
["_m":"CComponent":private]=>
NULL
}
我不知道在哪里得到那个未定义的id错误:(
答案 0 :(得分:2)
1.一种方法是改变
->select('AID, LastName, FirstName')
到
->select('AID as id, LastName, FirstName')
2.您也可以这样设置keyField
$arrayDataProvider=new CArrayDataProvider($rawData, array(
'id'=>'nameThisProvider',
'keyField' => 'AID'
'pagination'=>array(
'pageSize'=>10,
),
));
3.您可以使用CSqlDataProvider