带有空参数的 Yii2 搜索会抛出所有记录 [英] Yii2 search with empty param throws all records
问题描述
view2.php 中有 3 个搜索字段,当我在搜索字段中输入内容并点击搜索按钮时,搜索工作正常.但问题是当我点击搜索/回车按钮而不在搜索字段中输入任何内容时,它会显示与数据库中该模型相关的所有条目.
以下是我的机器型号:
公共函数搜索($params){$query = Supplier::find();$query->joinWith(['user', 'cities', 'industrialareas','supplierMachines', 'subCategory', 'types0','supplierCertificates' ]);$dataProvider = new ActiveDataProvider(['查询' =>$查询,'分页' =>['页面大小' =>20,],]);如果 (!($this->load($params) && $this->validate())) {$query->where('1 <> 1');}别的 {//网格过滤条件$query->andFilterWhere(['id' =>$this->id,'哟' =>$this->yoe,]);$query->andFilterWhere(['like', 'company_constitution', $this->company_constitution])->andFilterWhere(['like', 'street', $this->street])->andFilterWhere(['like', 'locality', $this->locality])}返回 $dataProvider;}}
调用view2函数的机器控制器(它显示搜索字段)
公共函数 actionView2(){//根据客户搜索显示机器$searchModel = new SupplierMachineSearch();$dataProvider = $searchModel->search(Yii::$app->request->queryParams);//使成为返回 $this->render('view2', ['搜索模型' =>$搜索模型,'数据提供者' =>$数据提供者,]);}
View2.php
<?= $form->field($searchModel, 'enter_city')->widget(AutoComplete::classname(), ['选项' =>['占位符' =>'选择一个城市...', 'class' =>'形式控制'],'clientOptions' =>['来源' =>ArrayHelper::getColumn($data, 'city_name'), ],]) ?><?= $form->field($searchModel, 'enter_iarea')->widget(AutoComplete::classname(), ['选项' =>['占位符' =>'选择一个 iarea ...', 'class' =>'形式控制'],'clientOptions' =>['来源' =>ArrayHelper::getColumn($data1, 'iarea_name'), ],]) ?><?= $form->field($searchModel, 'machine')->widget(AutoComplete::classname(), ['选项' =>['占位符' =>'选择一个 iarea ...', 'class' =>'形式控制'],'clientOptions' =>['来源' =>ArrayHelper::getColumn($all, 'name' ), ],]) ?><?= Html::activeHiddenInput($searchModel, 'id')?><div class="form-group"><?= Html::submitButton('Apply', ['class' =>'btn btn-success']) ?><?= Html::a('Reset', ['view2']);?>
<?php ActiveForm::end();?>
<?=ListView::widget(['数据提供者' =>$数据提供者,'itemView' =>'_viewmain','viewParams' =>['全视图' =>错误的,'上下文' =>'主页',],]);
检查加载后的搜索参数是否为空:
if ( !($this->load($params) && $this->validate()) 或(空($this->search_param1)和空($this->search_param2)和空($this->search_param3))){$query->where('1 <> 1');}
There are 3 search fields in view2.php, search is working properly when i enter something into search field and hit search button. But the problem is when I hit search/Enter button without entering anything into search fields, it displays all the entries related to that model from DB.
Below is my Machine model:
public function search($params)
{
$query = Supplier::find();
$query->joinWith(['user', 'cities', 'industrialareas','supplierMachines', 'subCategory', 'types0','supplierCertificates' ]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
if (!($this->load($params) && $this->validate())) {
$query->where('1 <> 1');
}
else {
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'yoe' => $this->yoe,
]);
$query->andFilterWhere(['like', 'company_constitution', $this->company_constitution])
->andFilterWhere(['like', 'street', $this->street])
->andFilterWhere(['like', 'locality', $this->locality])
}
return $dataProvider;
}
}
machine controller which calls view2 function(it displays search fields)
public function actionView2()
{
//Display machines based on the customer search
$searchModel = new SupplierMachineSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
// render
return $this->render('view2', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
View2.php
<?= $form->field($searchModel, 'enter_city')->widget(AutoComplete::classname(), [
'options' => ['placeholder' => 'Select a city ...', 'class' => 'form-control'],
'clientOptions' => [
'source' => ArrayHelper::getColumn($data, 'city_name'), ],
]) ?>
<?= $form->field($searchModel, 'enter_iarea')->widget(AutoComplete::classname(), [
'options' => ['placeholder' => 'Select a iarea ...', 'class' => 'form-control'],
'clientOptions' => [
'source' => ArrayHelper::getColumn($data1, 'iarea_name'), ],
]) ?>
<?= $form->field($searchModel, 'machine')->widget(AutoComplete::classname(), [
'options' => ['placeholder' => 'Select a iarea ...', 'class' => 'form-control'],
'clientOptions' => [
'source' => ArrayHelper::getColumn($all, 'name' ), ],
]) ?>
<?= Html::activeHiddenInput($searchModel, 'id')?>
<div class="form-group">
<?= Html::submitButton('Apply', ['class' => 'btn btn-success']) ?>
<?= Html::a('Reset', ['view2']);?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?=
ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_viewmain',
'viewParams' => [
'fullView' => false,
'context' => 'main-page',
],
]);
Check if the search parameters, once loaded, are empty:
if ( !($this->load($params) && $this->validate()) or
(empty($this->search_param1) and empty($this->search_param2) and empty($this->search_param3)) ) {
$query->where('1 <> 1');
}
这篇关于带有空参数的 Yii2 搜索会抛出所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!