0

リレーションテーブルからデータをフィルタリングできるcgridviewを作成しようとしています。CGridViewを使用するのはこれが初めてで、動作させることができません。ヘルプをいただければ幸いです。私には2つの問題があります。最初:ユーザーのドロップダウンフィルター(avtor、Novicaのリレーション)を取得できましたが、1つを選択すると、常に「結果が見つかりません」というメッセージが返されます。2番目:Novica.IDでフィルタリングしようとすると、次のエラーが発生します:(他の列でのフィルタリングはうまく機能します)

エラー500:

CDbException

CDbCommandはSQLステートメントの実行に失敗しました:SQLSTATE [23000]:整合性制約違反:1052where句の列'id'があいまいです。実行されたSQLステートメントは次のとおりです。SELECTCOUNT(*)FROM novica t LEFT OUTER JOIN uporabnik avtorON(tuporabnik_id= avtorid)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(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}

これらは私のテーブルです:

er

モデル:Novica(eng:ニュース)

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
    ),
));
4

2 に答える 2

2

問題1:

モデルでの使用

$criteria->compare('avtor.id', $this->uporabnik_id, true);

グリッドビューのドロップダウンで値として定義したものであるため、リレーションavtorのIDと比較してください。.. CHtml::listData(Uporabnik::model()->findAll(), 'id', 'upime')listData syntexはモデル、値、テキストです。

問題2:

モデルでの使用

$criteria->compare('t.id', $this->id);

tは、テーブルのyiiによって使用されるデフォルトのエイリアスです...あいまいな問題は、列idが両方のテーブルに存在することです。

于 2012-06-20T18:14:54.610 に答える
0

あなたのデータベースのmaybeeは外部キーを設定する必要があります; 更新時のカスケードと削除時のカスケード、この線量が機能しない場合、問題は正しく生成されているはずのモデルの関係にあると思われます

于 2013-05-20T07:46:25.623 に答える