Yii-如何从另一个模型获取数据 [英] Yii - How to get data from another model
问题描述
早上好
如何从另一个模型获取数据? 我有一个搜索字段,需要在其中搜索项目名称.然后我的Cgridview将显示选定的项目.
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.
我的关系中有这个
public function relations() {
return array(
'project' => array(self::BELONGS_TO, 'RmProjects', 'project_id'),
);
}
我试图在模型的搜索功能中访问project_name.
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无法执行以下SQL语句:SQLSTATE [42S22]: 未找到列:1054"where"中的未知列"project.project_name" 条款'.执行的SQL语句为:SELECT COUNT(DISTINCT
t
.id
)来自trx_evaluation_details
t
左外联接trx_evaluation
eval
ON(t
.eval_id
=eval
.id
)在哪里 (((project.project_name LIKE:ycp0)
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
) FROMtrx_evaluation_details
t
LEFT OUTER JOINtrx_evaluation
eval
ON (t
.eval_id
=eval
.id
) WHERE ((project.project_name LIKE :ycp0)
我的代码有什么问题??我尝试在当前模型中连接RmProject模型,以便可以访问project_name ..但是,出现此错误.请帮忙.
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 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);
这是我的搜索功能.
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,
));
}
推荐答案
我已经解决了这个问题.
I have solved this issue.
在我的Relations()中,我已经添加了这一行
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
这篇关于Yii-如何从另一个模型获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!