- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
早上好
如何从另一个模型获取数据?我有一个搜索字段,我需要在其中搜索项目名称。然后我的 Cgridview 将显示选定的项目。
我的关系里有这个
public function relations() {
return array(
'project' => array(self::BELONGS_TO, 'RmProjects', 'project_id'),
);
}
我试图在我的模型的搜索功能中访问 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,
));
}
当我这样做时,出现错误。
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.. 但是,我得到了这个错误。请帮助..
这是一个编辑:
这是我的全部关系部分
public function relations() {
return array(
'eval' => array(self::BELONGS_TO, 'Evaluation', 'eval_id'),
'project' => array(self::BELONGS_TO, 'RmProjects', 'project_id'),
);
}
我已经在我的模型中添加了它,但它仍然不起作用。它只是改变了表格。
$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,
));
}
最佳答案
您没有将模型添加到您的搜索方法
$criteria->together = true;
$criteria->with = array('eval','project');
$criteria->addSearchCondition('project.project_name', $search);
另外,通过方法的属性来设置搜索值也不是一个好的解决方案。定义新的模型公共(public)属性,定义规则并在搜索中使用它。
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);
}
关于php - Yii - 如何从另一个模型获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949387/
我是一名优秀的程序员,十分优秀!