2017-09-15 4 views
3

yii2でフィルタリングしようとしています。フォームフィールドに3つの入力(type = "radio")があり、各エントリはこの範囲の価格を持つ商品を検索する必要があります。ここでは、検索が実行されるコントローラのコードは次のとおりです。yii2をフィルタリングするとエラーが発生しますnullのメンバー関数isAttributeRequired()を呼び出す

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の価格を商品のテーブルから選択して、特定の推論をクリックすると、コントローラーの条件(つまり価格帯)で商品が表示されるようにします

答えて

0

フォームメソッドは "post"で、getによってデータをリトリーブしようとしています。 私はこのように変更されます。

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

私はあなたの問題を解決するかどうかわからないが、これは別のものです。

関連する問題