【问题标题】:How to merge filters from one search model to one findModel from a controller in Yii 2?如何在 Yii 2 中将一个搜索模型中的过滤器合并到一个控制器中的一个 findModel?
【发布时间】:2026-01-07 08:00:01
【问题描述】:

这个问题令人困惑,但我会解释一下。 我有来自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 都通过使用 Planificacion 中名为 idPlanificacion 的主键和 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

有什么解决办法吗?

【问题讨论】:

    标签: php yii2


    【解决方案1】:

    控制器内部的$this与控制器本身有关 但您指的是 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),
          ]);
      }
    

    【讨论】:

      最近更新 更多