I am making a Search query for a user to find a property within a price range. I do not know how Yii2 gets the user input. Here is my code for the form:
<?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(); ?>
And here is my model:
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;
}
}
I added the public $minprice and $maxprice as I was receiving this error:
Getting unknown property: app\models\PropertiesSearch::minprice
However, the query doesn't work, in my URL it shows the user input but the query isn't following through. I would have thought that ($model, 'minprice') gives the field a name and that $this-minprice gets that value. It works when I make public $minprice = '$100' and $maxprice = '$300', so they must be overwriting the users input, but if I remove them I get the previous error again, what am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire