简体   繁体   English

状况不佳在Yii2中不工作

[英]Not In Condition Not working in Yii2

I cleaned it up a bit, so the problem is why searchmodel returns room with id=30 when i say not to 我整理了一下,所以问题是为什么当我拒绝时,searchmodel返回id = 30的房间
searchModel Code 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;
}

Controller Action here is the controller action i dont think there is anything weird there just posting in case there is something wrong. Controller Action这里是控制器动作,我不认为有什么奇怪的地方,只是发布以防万一。

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,
    ]);
}
}

and the view code that shows the rooms is the code below: 显示房间的视图代码如下:

  <?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(); ?>

You have to close your if statement after $query->where('0=1'); 您必须在$query->where('0=1');之后关闭if语句$query->where('0=1'); , because it's only filtering when data in your model is not valid by $model->validate() . ,因为它仅在$model->validate()对模型中的数据无效时进行过滤。 It should look like this: 它看起来应该像这样:

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;
}

Reason is that you select only 原因是您仅选择

room_id ROOM_ID

that's why it does not see 这就是为什么它看不到

id ID

You can change first few lines of your code like that. 您可以像这样更改代码的前几行。

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

Your model search function should as : 您的模型搜索功能应为:

 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;
 }

Remove id param from first andFilterWhere function. 从第一个andFilterWhere函数中删除id参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM