gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:27:47 25 4
gpt4 key购买 nike

早上好

如何从另一个模型获取数据?我有一个搜索字段,我需要在其中搜索项目名称。然后我的 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) FROM trx_evaluation_details t LEFT OUTER JOIN trx_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,
));
}

enter image description here

最佳答案

您没有将模型添加到您的搜索方法

$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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com