在yii2中,搜索后是否只有1个结果如何自动进入详细视图页面。
谢谢。
答案 0 :(得分:1)
$dataprovider
后的控制器中:
$count=$dataProvider->getTotalCount();
if($count==1){
$dataProvider->getModels();
// get id and redirect view
//redirect to view
}else{
//redirect to gridview or listview
}
答案 1 :(得分:0)
我认为这是基于yii2 ..
的基本用法 $searchModel = new PostSearch(); // Search Model can be created by gii
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$count= $dataProvider->getTotalCount();
if($count==1){
$a = $dataProvider->getModels();
$id = $a[0]['id'] ; // accessing 2nd level array elements or $dataProvider->getModels()[0]['id'];
return $this->render('view', [
'model' => $this->findModel($id), //findModel($id) is protected function in the controller
]);
}