简体   繁体   中英

Yii CDbCriteria error: Column 'id' in where clause is ambiguous

I have a problem.

I use the ext.ecolumns.EColumnsDialog and when in these fields enters a value filter it works fine, except for id. When you type anything at id, whether true or false it receives something on the principle of alert () coming from javascript. This alert contains the text:

Error 500: <h1>CDbException</h1>
<p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `table_test` `t`  LEFT OUTER JOIN `two_table` `twotables` ON (`t`.`two_table`=`twotables`.`id`)  LEFT OUTER .....

How can I fix this error? I tried to add now $criteria->compare('t.id', $this->id); or similar.

EDIT1: my controller file:

$model = new Model_name('search');

        $model->unsetAttributes();  // clear any default values

        if (isset($_GET['Model_name'])){

            $model->attributes = $_GET['Model_name'];


}
        $this->render('admin', array(
            'model' => $model,
        ));

my Model file:

 public function search()
        {
     $criteria = new CDbCriteria;

 $criteria->addCondition("t.id=:id");
$criteria->params = array(":id"=>$this->id);
    ....

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort' => $sort,
            'pagination' => $pages
        ));

I changed Model name and controller because doesn't wish to disclose:)

That error is thrown when you are trying to reference a field where the field could belong to multiple tables you're selecting from, due to a missing table name or alias before the field.

Hard to tell without the entire query or without all of the CDBcriteria code. I'd try using the table name explicitly.

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

Try to use $criteria->addCondition() with binding parameter instead of $criteria->compare() as following:

$criteria->addCondition("t.id=:id");
$criteria->params = array(":id"=>$this->id);

You can replace all "compare" with "addCondition".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM