0

I am using Cgridview to display results for "User" model with relation from "UserFlag" model.

"User" model -> tbl_user (id, name, password, flag) "Flag" model -> tbl_userFlag (id, flag)

The id means the same from both models. However, the flags means differently (I can't modify the database so have to stick with it) and I need to display them on the same gridview.

The problem I encounter is that the gridview can show both flags correctly but it fails and shows error when I try to sort and filter the flag from "User" model. (However sorting and filtering work fine for the flag from "UserFlag" model.)

How can I solve it?

The error log:
CDbCommand failed to prepare the SQL statement: SQLSTATE[HY000]: General error: 1 ambiguous column name: flag.

"User" model:

class User extends CActiveRecord
{

public function relations()
{
    return array(
        'FLAG' => array(self::HAS_ONE, 'UserFlag','id'),
    );
}
public function search()
{
    $criteria=new CDbCriteria;
    $criteria->compare('id',$this->id);
    $criteria->compare('name',$this->username,true);
    $criteria->compare('password',$this->password,true);
    $criteria->compare('flag',$this->flag,true);
    $criteria->with = array(
        'FLAG' => array(
                'select' => 'FLAG.flag',
                'together' => true,
        )
    );
    $criteria->compare('FLAG.flag',$this->flagFromB,true);


    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'sort'=> array(
            'attributes'=>array(
                'flagFromB' => array(
                    'asc' => 'FLAG.flag',
                    'desc' => 'FLAG.flag DESC',
                ),
                '*',
            ),
        ),
    ));
}

"UserFlag" model:
Link to table tbl_userFlag (id, flag)

"User" view:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'username',
        'password',
        'email',
        'flag',

        array(
            'name' => 'flagFromB',
            'type' => 'raw',
            'value' => '$data->FLAG->flag',
        ),

        array(
            'class'=>'CButtonColumn',
        ),
    ),
));

1 Answers1

0

You can change your modal relationship.

User.php modal file.

<?php
class User extends CActiveRecord {

    public function relations() {
        return array(
            'FLAG' => array(self::BELONGS_TO, 'UserFlag', 'id'),
        );
    }
    public function search() {
        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('flag', $this->flag, true);
        $criteria->with = array(
            'FLAG' => array(
                'select' => 'FLAG.flag',
                'together' => true,
            )
        );
        $criteria->compare('FLAG.flag', $this->flagFromB, true);


        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort' => array(
                'attributes' => array(
                    'flagFromB' => array(
                        'asc' => 'FLAG.flag',
                        'desc' => 'FLAG.flag DESC',
                    ),
                    '*',
                ),
            ),
        ));
    }

}
?>

I hope it will help.

NikuNj Rathod
  • 1,663
  • 1
  • 17
  • 26