Php 如何从Yii 2中的控制器将一个搜索模型中的过滤器合并到一个findModel中?
这个问题令人困惑,但我会解释的。 我从Php 如何从Yii 2中的控制器将一个搜索模型中的过滤器合并到一个findModel中?,php,yii2,Php,Yii2,这个问题令人困惑,但我会解释的。 我从AsistenciaSearch.php public function search($params) { $query = Asistencia::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider([ 'query' => $q
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;
}
这是一个控制器函数,使用planificationcontroller.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和Planification都是通过使用Planification中名为IDPlanification
的主键和Asistencia中该模型中使用相同名称的外键来关联的。
问题是,我需要与另一个过滤器合并,其中findModel中的$id($id)类似于搜索查询中的$IDPlanification,如下所示:
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
有什么解决方案吗?$控制器内部的这个与控制器本身有关 但您所指的是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),
]);
}