我想在yii2搜索模型中编写一个mysql查询,但是在执行搜索条件时,它会给出连接错误。这是我的搜索模型。
class StudentRegistrationSearch extends StudentRegistration {
/**
* @inheritdoc
*/
public function rules() {
return [
[['id', 'student_id', 'recordstatus', 'addedbyuserid'], 'integer'],
[[ 'registration_date', 'dateadded', 'let'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios() {
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params) {
$query = StudentRegistration::find()->where(['recordstatus' => 1]);
$query = <<<EOD
SELECT
students.student_name,
students.`id`,
students.`reg_no`,
reg.`registration_date`,
exam.`exam_year`,
exam.`exam_title`
FROM students
LEFT JOIN student_registration reg ON (reg.`student_id` = students.`id`)
LEFT JOIN student_reg_detail detail ON(detail.`student_register_id` = reg.`id`)
LEFT JOIN def_exams exam ON(exam.`id` = detail.reg_exam_id)
WHERE students.`recordstatus` = 1 AND reg.`recordstatus` = 1 AND detail.`recordstatus` = 1
ORDER BY exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name; EOD;
$query = Yii::$app->db->createCommand($query);
$query = $query->queryAll();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'student_id' => $this->student_id,
'registration_date' => $this->registration_date,
'recordstatus' => $this->recordstatus,
'dateadded' => $this->dateadded,
'addedbyuserid' => $this->addedbyuserid,
'let' => $this->let,
]);
$query->orderBy('student_id');
return $dataProvider;
}}
我想在单个网格中显示来自多个表的数据,然后执行筛选操作,但简单查询不起作用。你能帮帮我吗。提前谢谢。
1条答案
按热度按时间pdsfdshx1#
试试下面的方法
或