我正在为用户进行搜索查询,以查找价格范围内的属性。我不知道yii2是如何获得用户输入的。这是我的表格代码:
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'address') ?>
<?= $form->field($model, 'minprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>
<?= $form->field($model, 'maxprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
这是我的模型:
class PropertiesSearch extends Properties
{
public $minprice;
public $maxprice;
public function search($params)
{
$query = Properties::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'NoofBedrooms' => $this->NoofBedrooms,
'type_id' => $this->type_id,
]);
$query->andFilterWhere(['like', 'address', $this->address])
->andFilterWhere(['like', 'city', $this->city])
->andFilterWhere(['like', 'Postcode', $this->Postcode])
->andFilterWhere(['>=', 'price', $this->minprice])
->andFilterWhere(['<=', 'price', $this->maxprice])
->andFilterWhere(['=', 'option', $this->option])
->andFilterWhere(['like', 'description', $this->description]);
return $dataProvider;
}
}
我在收到此错误时添加了public$minprice和$maxprice:
获取未知属性:app\models\propertiesarch::minprice
但是,查询不起作用,在我的url中,它显示用户输入,但查询没有完成。我本以为($model,'minprice')会给字段命名,而$this minprice会得到这个值。当我公开$minprice='$100'和$maxprice='$300'时,它起作用,因此它们必须覆盖用户输入,但是如果我删除它们,我会再次得到上一个错误,我做错了什么?
最佳答案
在model app\models\properties中尝试放置属性$minprice和$maxprice
并添加条件验证。
class Properties extends ActiveRecord
{
public $minprice;
public $maxprice;
...