简体   繁体   中英

Yii Get data from another model using Relation

I am trying to filter a table in my view using the search function in my model. I have two models which i need to get data from model Evaluation to another model EvaluationDetails .

i have this in my EvaluationDetails model

public function relations() {
        return array(
            'eval' => array(self::BELONGS_TO, 'Evaluation', 'eval_id'),
        );
    }

i also have this in my search function

public function search($employee = '', $search_date_start = '', $search_date_end = '', $search = '') {

        $criteria = new CDbCriteria;
        $criteria->with = array('eval' => array('together' => true));
        $criteria->compare('employee_id', $this->employee_id);
        $criteria->compare('remarks', $this->remarks, true);
        $criteria->compare('eval_id', $this->eval_id);
        $criteria->compare('eval.evaluatee', $this->evaluatee_search);            

        $criteria->addSearchCondition('eval.evaluatee', $search);

        if ($employee != '')
            $criteria->compare('employee_id', $employee->company_id);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

I am trying to filter a table where in the user will search for a name, pass the value in $search which is then used in the search function in my EvaluationDetails model With this, i am getting an error.

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'employee_id' in where clause is ambiguous. The SQL statement executed was: SELECT COUNT(DISTINCT t . id ) FROM trx_evaluation_details t LEFT OUTER JOIN trx_evaluation eval ON ( t . eval_id = eval . id ) WHERE ((eval.evaluatee LIKE :ycp0) AND (employee_id=:ycp1))

what seems to be the problem with my code. please help..

Seeing from the error message, it looks like both EvaluationDetails and Evaluation have a field call "employee_id" (i am not sure)

But if so, you need to replace the

 $criteria->compare('employee_id', $employee->company_id);

with

 $criteria->compare('t.employee_id', $employee->company_id);

To explicitly tell yii the field is from EvaluationDetails table.

BTW, u are using $employee->company_id (should be $employee->employee_id ?)

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