如何在Yii 2中将一个搜索模型中的过滤器合并到一个控制器中的一个findModel?

时间:2017-02-22 20:04:43

标签: php yii2

问题很混乱,但我会解释。 我有来自AsistenciaSearch.php

的搜索查询
public function search($params)
    {
        $query = Asistencia::find();

        // add conditions that should always apply here

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

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->joinWith('rutAlumno0');
        $query->joinWith('idPlanificacion0');

        // grid filtering conditions
        $query->andFilterWhere([
            'idAsistencia' => $this->idAsistencia,
            //'idPlanificacion' => $this->idPlanificacion,
        ]);

        $query->andFilterWhere(['like', 'asistencia', $this->asistencia])
            ->andFilterWhere(['like', 'rutAlumno', $this->rutAlumno])
            //->andFilterWhere(['like', 'idPlanificacion', $this->idPlanificacion])
            ->andFilterWhere(['like', 'alumno.nombreAlumno', $this->nombreAlumno])
            ->andFilterWhere(['like', 'alumno.apellidoAlumno', $this->apellidoAlumno])
            ->andFilterWhere(['like', 'alumno.cursoAlumno', $this->cursoAlumno])
            ->andFilterWhere(['like', 'alumno.establecimientoAlumno', Yii::$app->user->identity->escuelaProfesor]);


        return $dataProvider;
    }

这是一个控制器功能,使用PlanificacionController.php中的搜索查询:

public function actionVerasistencia($id)
    {
        $searchModel = new AsistenciaSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('verasistencia', [
            'model' => $this->findModel($id), //findModel from Planificacion 
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

Asistencia和Planificacion都是通过使用名为idPlanificacion的Planificacion中的主键和使用相同名称的Asistencia中该模型的外键来关联的。 问题是,我需要与另一个过滤器合并,其中来自findModel($ id)的$ id类似于搜索查询中的$ idPlanificacion,如下所示:

public function actionVerasistencia($id)
    {
        $searchModel = new AsistenciaSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('verasistencia', [
            'model' => $this->findModel($id),
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider->andFilterWhere('like',$id,$this->idPlanificacion),
        ]);
    }

但我收到了这个错误:

Getting unknown property: frontend\controllers\PlanificacionController::idPlanificacion

有任何解决方案吗?

1 个答案:

答案 0 :(得分:1)

$控制器内部的这个与控制器本身有关 但是您指的是idPlanificacion别名,指的是模型属性

可能是你想要通过模型来追溯价值,例如:

$model = $this->findModel($id)

所以可能

public function actionVerasistencia($id)
  {
      $searchModel = new AsistenciaSearch();
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

      $model = $this->findModel($id);
      return $this->render('verasistencia', [
          'model' =>$model,
          'searchModel' => $searchModel,
          'dataProvider' => $dataProvider->andFilterWhere('like',$id,$model->idPlanificacion),
      ]);
  }