如何在yii2搜索模型中编写mysql查询

时间:2018-06-22 11:34:15

标签: php mysql yii2

我想在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 个答案:

答案 0 :(得分:1)

尝试类似的操作

$query = (new yii\db\Query())
        ->from(['s' => 'students'])
        ->select(['s.student_name', 's.id', 's.reg_no', 'reg.registration_date', 'exam.exam_year', 'exam.exam_title'])
        ->leftJoin(['reg' => 'student_registration', 'stu.student_id = s.id'])
        ->leftJoin(['detail' => 'student_reg_detail', 'stu.student_id = s.id'])
        ->leftJoin(['exam' => 'def_exams ', 'exam.id = detail.reg_exam_id'])
        ->where(['s.recordstatus' => 1, 'reg.recordstatus' => 1, 'detail.recordstatus' => 1])
        ->orderBy('exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name')
;

OR

$query = Students::find()
        ->from(['s' => Students::tablename()])
        ->select(['s.student_name', 's.id', 's.reg_no', 'reg.registration_date', 'exam.exam_year', 'exam.exam_title'])
        ->leftJoin(['reg' => 'student_registration', 'stu.student_id = s.id'])
        ->leftJoin(['detail' => 'student_reg_detail', 'stu.student_id = s.id'])
        ->leftJoin(['exam' => 'def_exams ', 'exam.id = detail.reg_exam_id'])
        ->where(['s.recordstatus' => 1, 'reg.recordstatus' => 1, 'detail.recordstatus' => 1])
        ->orderBy('exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name')
;


$dataProvider = new ActiveDataProvider([
    'query' => $query,
        ]);