我正在尝试创建一个可以过滤关系表数据的cgridview。这是我第一次使用CGridView,我无法让它工作。感谢帮助。 我有2个问题。第一:我设法获得了我的用户的下拉过滤器(avtor,Novica中的关系),但是当我选择一个时,它总是返回:“没有找到结果。”。 第二:当我尝试使用Novica.ID进行过滤时,我收到此错误:(使用其他columsn过滤效果很好)
错误500:
CDbException
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 \框架\分贝\ 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(Object(CDbCriteria)) 3 C:\xampp\htdocs\PEF\framework\db\ar\CActiveRecord.php(1540): CActiveFinder->count(Object(CDbCriteria)) 4 C:\xampp\htdocs\PEF\framework\web\CActiveDataProvider.php(179): CActiveRecord->count(Object(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): require('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(Array) 19 C:\xampp\htdocs\PEF\framework\web\CController.php(287): CController->runAction(Object(CInlineAction)) 20 C:\xampp\htdocs\PEF\framework\web\CController.php(266): CController->runActionWithFilters(Object(CInlineAction), Array) 21 C:\xampp\htdocs\PEF\framework\web\CWebApplication.php(276): CController->run('') 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}
这些是我的表格:
型号: 诺维卡(英国:新闻)
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
),
));
答案 0 :(得分:2)
问题1:
在您的模型中使用
$criteria->compare('avtor.id', $this->uporabnik_id, true);
与关系avtor的id进行比较,因为这是你在gridview中的下拉列表中定义的值。CHtml::listData(Uporabnik::model()->findAll(), 'id', 'upime')
.. listData syntex是模型,值,文本..
问题2:
在您的模型中使用
$criteria->compare('t.id', $this->id);
t是yii用于表的默认别名... ambigious的问题是两个表中都存在列id
..
答案 1 :(得分:0)