过滤yii2会产生错误在null

时间:2017-09-15 12:27:11

标签: php filter yii2

我试图过滤yii2。其中有一个表单字段3输入(type =" radio"),每个条目都应搜索价格在此范围内的产品。以下是执行搜索的控制器代码:

 public function actionFilter()
        {
            $filter = trim(Yii::$app->request->get('filter'));
            $this->setMeta('MAC-SHOPPER | ' . $filter);
            if (!$filter) {
                return $this->render('filter');
            }
/*
            if ($filter <= 15) {

            $query = Product::find()->where(['<=', 'price', 15]);
            }*/

            $model = new Product();
            if($Button1) {
                $query = Product::find()->where(['between', 'price', "0", "50" ])->all();
            }

            $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => 2, 'forcePageParam' => false, 'pageSizeParam' => false]);

            $products = $query->offset($pages->offset)->limit($pages->limit)->all();
            return $this->render('filter', compact('products', 'pages', 'filter', 'model'));
        }

产品型号代码:

<?php
    namespace app\models;
    use yii\db\ActiveRecord;
    class Product extends ActiveRecord
    {
        public $Button1;
        public $Button2;
        public $Button3;
        public $radioButtonList;


             public function behaviors()
        {
            return [
                'image' => [
                    'class' => 'rico\yii2images\behaviors\ImageBehave',
                ]
            ];
        }




        public static function tableName()
        {
            return 'product';
        }

        public function getCategory()
        {

            return $this->hasOne(Category::className(), ['id' => 'category_id']);
        }



    }
?>

表单本身的代码:

<?php $form = ActiveForm::begin([
                                'id' => 'task-form',
                                'action' => \yii\helpers\Url::to(['category/filter']),
                                ]  
                                )?>





                         <?= $form->field($model, 'radioButtonList')
                                ->radioList([
                                    'Button1' => 'от 0-1500',     
                                    'Button2' => 'от 3000-5000',
                                    'Button3' => 'от 5000-20000'
                                ],[
                                    'id' => 'radio_button',

                                ]); ?>
                            <?= Html::submitButton('Найти', ['class' => 'btn btn-success']);?>
                        <?php $form = ActiveForm::end() ?>

如何从商品表中输入属性$ Button1,$ Button2,$ Button3价格,这样当我点击某个推理时,他会在控制器的状态下显示商品(也就是说,按价格范围计算)

1 个答案:

答案 0 :(得分:0)

表单方法是&#34; post&#34;并且您尝试通过get来重新获取数据。 我会这样改变:

public function actionFilter()
{
   $filter = trim(Yii::$app->request->post('filter'));
   $this->setMeta('MAC-SHOPPER | ' . $filter);
   if (!$filter) {
      return $this->render('filter');
   }
........
}

我不确定你是否会解决你的问题,但这是另一个问题。