Hi guys, following code should search records for date. But whatever I click,else branch in condition will be executed,so I will get outprinting:
choice_date is true
.It will be searched for records which are <=department_created_date,but never for records >=department_created_date
Any ideas,how to fix this?
Here is my model:
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
class RadioForm extends Model{
public $choice_date;
public function rules() {
return [
['choice_date', 'boolean']
];
}
}
?>
Here is my RadioList
$form = ActiveForm::begin();
$model = new backend\models\RadioForm();
?>
<?= $form->field($model, 'choice_date')->radioList(array('0' => 'Before', '1' => 'After'))->label('Please, choose Datesearching!'); ?>
and here is my searching class:
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Departments;
class DepartmentsSearch extends Departments {
public $choice_date;
public function rules() {
return [
[['department_id'], 'integer'],
[['choice_date'], 'boolean'],
[['department_name', 'department_created_date', 'department_status', 'companies_company_id', 'branches_branch_id'], 'safe'],
];
}
public function scenarios() {
return Model::scenarios();
}
public function search($params) {
$query = Departments::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate())
return $dataProvider;
/*
Whatever I click in RadioList,property will be not 0,so I'll get setFlash->'choice_date is true'
*/
if ($this->choice_date == "0") {
Yii::$app->session->setFlash('success', 'choce_date is false');
$query->andFilterWhere(['<=', 'department_created_date', $this->department_created_date]);
} else {
Yii::$app->session->setFlash('success', 'choice_date is true');
$query->andFilterWhere(['>=', 'department_created_date', $this->department_created_date]);
}
$query->joinWith('companiesCompany');
$query->joinWith('branchesBranch');
$query->andFilterWhere(['like', 'department_name', $this->department_name])
->andFilterWhere(['like', 'companies.company_name', $this->companies_company_id])
->andFilterWhere(['like', 'branches.branch_name', $this->branches_branch_id])
->andFilterWhere(['like', 'department_status', $this->department_status]);
return $dataProvider;
}
}