不在条件下在Yii2中不起作用

时间:2017-08-07 13:39:12

标签: php mysql yii2

我把它清理了一下,所以问题是为什么searchmodel返回id = 30的房间,当我说不要 searchModel代码

public function search($params)
{
   $ids = [];
   $ids[]=30;
    $query = room::find();
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);
    $this->load($params);
    if (!$this->validate()) {

     }
 $query->andFilterWhere([
        'price' => $this->price,
        'category_id' => $this->category_id,
        'id' => $this->id,
        'ci_id'=>$this->ci_id;

    ]);
    $query->andFilterWhere(['not in','id',$ids]);
    return $dataProvider;
}

控制器动作这里是控制器动作我不认为在发生错误的情况下发布任何奇怪的内容。

public function actionIndex()
{
    $searchModel = new roomSearch();
    //$user = Yii::$app->user->identity;

    if ($searchModel->load(Yii::$app->request->post()) ) {
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}
else{

  //

    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}
}

,显示房间的视图代码如下代码:

  <?php \yii\widgets\Pjax::begin(['timeout' => 30000, 'clientOptions' => ['container' => 'pjax-container']]); 
?>

    <?php $form = ActiveForm::begin([ 'action' => ['index'],
'method' => 'get','options' => ['data-pjax' => true ],]); ?>
 <?= $form->field($searchModel, 'category_id')->dropdownList(MtCat::getHierarchy(false, [], false, false),['onchange'=>'this.form.submit()','prompt' => 'Όλες οι κατηγορίες']); ?>
 <?= $form->field($searchModel, 'ci_id')->dropdownList(City::getList(),['onchange'=>'this.form.submit()','prompt' => 'Όλες οι περιοχές']); ?>

   <?php ActiveForm::end(); ?>
</br>


<?=

ListView::widget([

    'layout' => "{pager}\n{summary}\n<div class='all-items'>{items}</div>\n{pager}",

    //'layout' => "{pager}\n<div class='all-items'>{items}</div>\n{pager}",

    'summary' => sprintf('<div class="summary"><b>%s</b> αποτελέσματα δωματίων</div>', $dataProvider->totalCount),

    'dataProvider' => $dataProvider,

     'showOnEmpty' => false,


    'itemOptions' => ['class' => 'item'],

    'itemView' => '_img', /* function ($model, $key, $index, $widget) { return Html::a(Html::encode($model->id), ['view', 'id' => $model->id]); }, */

    'pager' => [

        'maxButtonCount' => ceil($dataProvider->totalCount / $dataProvider->pagination->pageSize),

    ],

]);

?>
 <?php \yii\widgets\Pjax::end(); ?>

3 个答案:

答案 0 :(得分:0)

您必须在if之后关闭$query->where('0=1');语句,因为它仅在$model->validate()模型中的数据无效时才会过滤。它应该是这样的:

public function search($params)
{
   $ids = [];
   $ids[]=30;
   $query = room::find();
   $dataProvider = new ActiveDataProvider([
        'query' => $query,
   ]);
   $this->load($params);
   if (!$this->validate()) {

   } // here you need close if condition
   $query->andFilterWhere([
        'price' => $this->price,
        'category_id' => $this->category_id,
        'id' => $this->id,
        'ci_id'=>$this->ci_id;

   ]);

   $query->andFilterWhere(['not in','id',$ids]);

   return $dataProvider;
}

答案 1 :(得分:0)

原因是您只选择

  

ROOM_ID

这就是为什么它没有看到

  

ID

您可以更改代码的前几行。

$ids = [];
$ids[] = 30;
$results = Yii::$app->db->createCommand('select id,room_id from y2gall_reservations')->queryAll();
$query = room::find();

答案 2 :(得分:0)

您的模型搜索功能应为:

 public function search($params)
 {
   $ids = [];
   $ids[]=30;
   $query = room::find();
   $dataProvider = new ActiveDataProvider([
      'query' => $query,
   ]);
   $this->load($params);
   if (!$this->validate()) {

   }
   $query->andFilterWhere([
     'price' => $this->price,
     'category_id' => $this->category_id,

     'ci_id'=>$this->ci_id;

   ]);
   $query->andFilterWhere(['not in','id',$ids]);
   return $dataProvider;
 }

从第一个andFilterWhere函数中删除id param。