简体   繁体   中英

Yii - How to get data from another model

Good morning

How do I get a data from another model.? I have a search field where I need to search a project name. then my Cgridview will display the selected projects.

I have this in my relations

public function relations() {
        return array(
            'project' => array(self::BELONGS_TO, 'RmProjects', 'project_id'),
        );
    }

i tred to access project_name in the search function in my model..

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

        //I tried it like this
        $criteria->addSearchCondition('project.project_name', $search);           

        if ($employee != '') {
            $criteria->compare('t.employee_id', $employee->company_id);
        }    
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

when I do this, I get an error.

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'project.project_name' in 'where clause'. 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 ((project.project_name LIKE :ycp0)

what is wrong with my code.? I tried connecting the RmProject model in my current model so that I can access the project_name.. but instead, I get this error. Please help..

This is an edit:

This is my whole relations part

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

I have added this in my model but it still did not work. it just changed the table.

$criteria->with = array('project' => array('together' => true));     
$criteria->addSearchCondition('project.project_name', $search);

this is 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->with = array('project' => array('together' => true));                       

        $criteria->compare('employee_id', $this->employee_id);
        $criteria->compare('remarks', $this->remarks, true);
        $criteria->compare('eval_id', $this->eval_id);            

        $criteria->addSearchCondition('project.project_name', $search);
        $criteria->addSearchCondition('start_date', $search_date_start, 'AND');
        $criteria->addSearchCondition('end_date', $search_date_end, 'AND');

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

         if ($search_date_end !== '' && $search_date_start !== '' && $search !== '') {
                $criteria->condition = "start_date  >= '$search_date_start' AND end_date <= '$search_date_end' AND project.project_name like '%$search%'AND t.employee_id = '$employee->company_id'";                
            }

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

在此处输入图片说明

You didn't add model to your search method

$criteria->together = true;
$criteria->with = array('eval','project');
$criteria->addSearchCondition('project.project_name', $search);

Also it's not a good solution to set searching value via method's attributes. Define new model public attribute, define a rule and use it in search.

public $project_name;

public function attributeLabels(){
   return array(
       //... your other labels there
       'project_name' => 'Project Name',
   );
}

public function rules(){
   return array(
      //... your other rules there
      array('project_name', 'safe', 'on' => 'search'),
   );
}

public function search(){
   $criteria->compare('project.project_name', $this->project_name);
}

I have solved this issue.

in my relations(), i have added this line

'project' => array(self::HAS_ONE, 'RmProjects', array ('project_id'=>'project_id'), 'through'=> 'eval'),

this joins the models that has a connection that passes through another model. and then you can get the data from another model.. i hope that this answer can help anybody who has the same question as me.

you can read it in here.. which is in relational query using through http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through

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