RadioList似乎没有呈现

时间:2017-07-29 18:02:37

标签: yii2

以下代码应搜索日期记录。但无论我点击什么,代码中的else分支都将被处理

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

模型:

&#13;
&#13;
<?php

namespace backend\models;

use Yii;
use yii\base\Model;

class RadioForm extends Model { // A new Class programmed by Thomas Kipp 

    public $choice_date;


    public function rules() {
        return [
            ['choice_date', 'boolean'],
        ];
    }

}
?>
&#13;
&#13;
&#13;

搜索级:

&#13;
&#13;
<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Departments;

class DepartmentsSearch extends Departments {

    public function rules() {
        return [
            [['department_id'], 'integer'],
            [['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;
      
        $model = new RadioForm();
        $model->load($params);
        if ($model->choice_date == true)
            $query->andFilterWhere(['<', 'department_created_date', $this->department_created_date]);
        else
            $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;
    }

}
&#13;
&#13;
&#13;

P.S。:我能够影响日期过滤更改属性true / false,例如:

    public $choice_date=true;

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

任何想法,如何解决这个问题?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

1 个答案:

答案 0 :(得分:1)

RadioForm为默认值为0的属性$choice_date建模(最初编写public $choice_date = 0;)。在你写的search()方法中:

$model = new RadioForm();
if ($model->choice_date == 0)
/* ... */

理论上总是返回true,因为您尚未为其分配任何值。您在$choice_date模型中创建了变量RadioForm,因此您还需要添加一行:

$model = new RadioForm();
$model->load($params);

if ($model->choice_date == 0)
/* ... */

假设$paramsRadioForm模型的数组,这会将所选值加载到$choice_data属性,并会产生影响。