So I’m setting a search form for a user to find a property based on their input, however I’m having some problems at getting some of the users input, here is my form code:
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id')->dropDownList(['' => 'All']) ?>
<?= $form->field($model, 'address') ?>
<?= $form->field($model, 'city') ?>
<?= $form->field($model, 'Postcode') ?>
<?= $form->field($model, 'minprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>
<?= $form->field($model, 'maxprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>
<?php echo $form->field($model, 'option') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
and then my model code:
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(['>=', 'minprice', $this->minprice])
->andFilterWhere(['<=', 'maxprice', $this->maxprice])
->andFilterWhere(['=', 'option', $this->option])
->andFilterWhere(['like', 'description', $this->description]);
return $dataProvider;
}
I want the user to be able to search for a property within a price range, so have have two fields, minimum price ‘minprice’ and maximum price ‘maxprice’. I would have thought that in my form, '($model, ‘minprice’) sets the name to minprice and in my model $this->minprice gets this value, however I get this error
Getting unknown property: app\models\PropertiesSearch::minprice
I only have price as an attribute in my table and mot minprice or maxprice, so am I doing something wrong or does Yii2 have a query to help?