我正在尝试制作一个可以从关系表中过滤数据的 cgridview。这是我第一次使用 CGridView,但我无法让它工作。帮助表示赞赏。我有2个问题。首先:我设法获得了我的用户的下拉过滤器(avtor,Novica 中的关系),但是当我选择一个时,它总是返回:“未找到结果。”。第二:当我尝试使用 Novica.ID 进行过滤时,出现此错误:(使用其他列进行过滤效果很好)
错误 500:
CDb异常
CDbCommand 未能执行 SQL 语句:SQLSTATE [23000]:完整性约束违规:1052 where 子句中的列 'id' 不明确。执行的 SQL 语句是:SELECT COUNT(*) FROM
novica
t
LEFT OUTER JOINuporabnik
avtor
ON (t
.uporabnik_id
=avtor
.id
) WHERE ((uporabnik_id = :id) AND (id=:ycp0)) (C:\xampp\htdocs\PEF\framework\db \CDbCommand.php:528)0 C:\xampp\htdocs\PEF\framework\db\CDbCommand.php(425): CDbCommand->queryInternal('fetchColumn', 0, Array) 1 C:\xampp\htdocs\PEF\framework\db\ar\CActiveFinder.php(762): CDbCommand->queryScalar() 2 C:\xampp\htdocs\PEF\framework\db\ar\CActiveFinder.php(155): CJoinElement->count(对象(CDbCriteria)) 3 C:\xampp\htdocs\PEF\framework\db\ar\CActiveRecord.php(1540): CActiveFinder->count(对象(CDbCriteria)) 4 C:\xampp\htdocs\PEF\framework\web\CActiveDataProvider.php(179): CActiveRecord->count(对象(CDbCriteria)) 5 C:\xampp\htdocs\PEF\framework\web\CDataProvider.php(193): CActiveDataProvider->calculateTotalItemCount() 6 C:\xampp\htdocs\PEF\framework\web\CActiveDataProvider.php(129): CDataProvider->getTotalItemCount() 7 C:\xampp\htdocs\PEF\framework\web\CDataProvider.php(137): CActiveDataProvider->fetchData() 8 C:\xampp\htdocs\PEF\framework\zii\widgets\CBaseListView.php(105): CDataProvider->getData() 9 C:\xampp\htdocs\PEF\framework\zii\widgets\grid\CGridView.php(269): CBaseListView->init() 10 C:\xampp\htdocs\PEF\framework\web\CBaseController.php(148): CGridView->init() 11 C:\xampp\htdocs\PEF\framework\web\CBaseController.php(173): CBaseController->createWidget('zii.widgets.gri...', Array) 12 C:\xampp\htdocs\PEF\protected\views\adminNovica\index.php(28): CBaseController->widget('zii.widgets.gri...', Array) 13 C:\xampp\htdocs\PEF\framework\web\CBaseController.php(127): 要求('C:\xampp\htdocs...') 14 C:\xampp\htdocs\PEF\framework\web\CBaseController.php(96): CBaseController->renderInternal('C:\xampp\htdocs...', Array, true) 15 C:\xampp\htdocs\PEF\framework\web\CController.php(870): CBaseController->renderFile('C:\xampp\htdocs...', Array, true) 16 C:\xampp\htdocs\PEF\protected\controllers\AdminNovicaController.php(19): CController->renderPartial('index', Array) 17 C:\xampp\htdocs\PEF\framework\web\actions\CInlineAction.php(50): AdminNovicaController->actionIndex() 18 C:\xampp\htdocs\PEF\framework\web\CController.php(309): CInlineAction->runWithParams(数组) 19 C:\xampp\htdocs\PEF\framework\web\CController.php(287): CController->runAction(对象(CInlineAction)) 20 C:\xampp\htdocs\PEF\framework\web\CController.php(266): CController->runActionWithFilters(对象(CInlineAction),数组)21 C:\xampp\htdocs\PEF\framework\web\CWebApplication.php(276): CController->运行('')22 C:\xampp\htdocs\PEF\framework\web\CWebApplication.php(135): CWebApplication->runController('adminnovica') 23 C:\xampp\htdocs\PEF\framework\base\CApplication.php(162): CWebApplication->processRequest() 24 C:\xampp\htdocs\PEF\index.php(13): CApplication->run() 25 {main}
这些是我的表:
型号: Novica(英文:新闻)
public function relations() {
return array(
'uporabnik' => array(self::BELONGS_TO, 'DostopNovica', 'uporabnik_id'),
'avtor' => array(self::BELONGS_TO, 'Uporabnik', 'uporabnik_id'),
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->with = 'avtor';
$criteria->compare('naslov', $this->naslov, true);
$criteria->compare('datum', $this->datum, true);
$criteria->compare('avtor.upime', $this->uporabnik_id, true); //avtor.upime (eng. author.username)
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'datum DESC', //date
),
'pagination' => array(
'pageSize' => 5
),
));
}
行动:
public function actionIndex() {
$model = new Novica('search');
if (isset($_GET['Novica']))
$model->attributes = $_GET['Novica'];
$params = array(
'model' => $model,
);
if (!isset($_GET['ajax']))
$this->render('index', $params);
else
$this->renderPartial('index', $params);
}
CGridView:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array('id',
array('name' => 'datum',
'value' => 'date("j.n.Y G:i", strtotime($data->datum))'
),
array(
'name' => 'uporabnik_id',
'value' => 'Uporabnik::Model()->FindByPk($data->uporabnik_id)->upime',
'filter' => CHtml::listData(Uporabnik::model()->findAll(), 'id', 'upime'),
),
'naslov'
//specify the colums you wanted here
),
));