简体   繁体   English

Yii-如何从另一个模型获取数据

[英]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. 然后我的Cgridview将显示选定的项目。

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.. 我试图在模型的搜索功能中访问project_name。

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'. CDbCommand无法执行SQL语句:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ project.project_name”。 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) 执行的SQL语句是:SELECT COUNT(DISTINCT tid )FROM trx_evaluation_details t LEFT OUTER JOIN trx_evaluation eval ON( teval_id = evalid )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. 我尝试在当前模型中连接RmProject模型,以便可以访问project_name ..但是,出现此错误。 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 在我的Relations()中,我添加了这一行

'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 您可以在此处阅读它。. 通过 http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through 使用关系查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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