我在试着过滤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 price,以便当我单击某个推断时,他显示商品,就像在控制器的条件下一样(即按价格范围)。

最佳答案

表单方法是在“post”中,您尝试通过get检索数据。
我会这样改变:

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

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

关于php - 过滤yii2会产生错误调用null的成员函数isAttributeRequired(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46239592/

10-13 00:53