Searching records for date fails

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;

    }


}



It could be that choice_date is being returned from the form as a string ‘0’, not the int 0.

Well, if I change condition like this…




      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]);

        }



…whatever I select in Radio list, this time else-branch will be executed,so I will get outprinting:




choice_date is true



Now, it will be searched for records which are >=department_created_date,but never for records <= department_created_date.

Obviously, choice_date(property of Radio Button) will not be rendered from formular, only in searching class. Here is my formular(index.php in folder departments)




<?php


use yii\helpers\Html;

use yii\grid\GridView;

use yii\widgets\ActiveForm;


$this->title = Yii::t('app', 'Departments');

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="departments-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <?php

    $form = ActiveForm::begin();

    $model = new backend\models\DepartmentsSearch();

    ?><?= $form->field($model, 'choice_date')->radioList(array(0 => 'Before', 1 => 'After'))->label('Please, choose Datesearching!'); ?>

    <p><?= Html::a(Yii::t('app', 'Create Departments'), ['create'], ['class' => 'btn btn-success']) ?></p>

    <?=

    GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

            'branchesBranch.branch_name',

            'department_name',

            'companiesCompany.company_name',

            'department_created_date',

            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]);

    ?>

</div>



It looks like that you are using 2 different model instances in the view and in the controller.

Try making your view and models match what the other is looking for


'0' => 'Before', '1' => 'After'


$this->choice_date === '0'

Well, this forum was not able to solve my problem, but stackoverflow did. Here is code of all relevant instances, maybe it’s useful:

RadioList:




    <?php $form = ActiveForm::begin();?>

    <?= $form->field($searchModel, 'choice_date')->radioList([0 => 'Before', 1 => 'After'], 

            ['itemOptions' => ['class' => 'choiceRadio']])->label('Please, choose Datesearching!');

    ActiveForm::end();?>



SearchingClass:




<?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', 'branches_branch_id', 'companies_company_id'], 'integer'],

            [['department_name', 'department_created_date', 'department_status'], 'safe'],

            [['choice_date'], 'boolean'],

        ];

    }


    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;

        }

        $query->andFilterWhere([

            'department_id' => $this->department_id,

            'branches_branch_id' => $this->branches_branch_id,

            'companies_company_id' => $this->companies_company_id,

        ]);


        if ($this->choice_date == 0) {

            Yii::$app->session->setFlash('success', 'choice_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', 'department_status', $this->department_status]);


        return $dataProvider;

    }


}




GridView:




GridView::widget([

    'dataProvider' => $dataProvider,

    'filterModel' => $searchModel,

    'filterSelector' => '.choiceRadio',

.

.

.