我正在尝试在 yii2 上进行过滤。其中有一个表单域 3 input (type = "radio"),每个条目应该搜索价格在此范围内的产品。这是执行搜索的 Controller 代码:
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价格,这样当我点击某个推理时,他显示商品在controller的条件下完成(即通过价格范围)
最佳答案
表单方法在“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产生错误Call to a member function isAttributeRequired() on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239592/