yii2 - 无法在_search.php中的日期之间进行搜索

时间:2017-12-15 09:42:55

标签: php yii2 kartik-v

似乎找不到我做错了什么。 我的搜索模型:     

namespace app\models\searches;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Slas;

/**
 * SlasSearch represents the model behind the search form about 
`app\models\Slas`.
 */
class SlasSearch extends Slas
{
/**
 * @var string
 */
public $dateStart;
/**
 * @var string
 */
public $dateEnd;

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['id', 'pc_menor_igual_10', 'pc_maior_10_menor_igual_20', 
'pc_maior_20_menor_igual_30', 'pc_maior_30', 'ps_menor_igual_25', 
'ps_maior_25_menor_igual_35', 'ps_maior_35_menor_igual_45', 'ps_maior_45', 
'cc_menor_igual_5', 'cc_maior_5_menor_igual_10', 
'cc_maior_10_menor_igual_20', 'cc_maior_20', 'cs_menor_igual_25', 
'cs_maior_25_menor_igual_35', 'cs_maior_35_menor_igual_45', 'cs_maior_45', 
'c90_menor_igual_15', 'c90_maior_15_menor_igual_20', 
'c90_maior_20_menor_igual_30', 'c90_maior_30'], 'integer'],
        [['data'], 'safe'],
    ];
}

/**
 * @inheritdoc
 */
public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = Slas::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any 
records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'id' => $this->id,
        'data' => $this->data,
        'pc_menor_igual_10' => $this->pc_menor_igual_10,
        'pc_maior_10_menor_igual_20' => $this->pc_maior_10_menor_igual_20,
        'pc_maior_20_menor_igual_30' => $this->pc_maior_20_menor_igual_30,
        'pc_maior_30' => $this->pc_maior_30,
        'ps_menor_igual_25' => $this->ps_menor_igual_25,
        'ps_maior_25_menor_igual_35' => $this->ps_maior_25_menor_igual_35,
        'ps_maior_35_menor_igual_45' => $this->ps_maior_35_menor_igual_45,
        'ps_maior_45' => $this->ps_maior_45,
        'cc_menor_igual_5' => $this->cc_menor_igual_5,
        'cc_maior_5_menor_igual_10' => $this->cc_maior_5_menor_igual_10,
        'cc_maior_10_menor_igual_20' => $this->cc_maior_10_menor_igual_20,
        'cc_maior_20' => $this->cc_maior_20,
        'cs_menor_igual_25' => $this->cs_menor_igual_25,
        'cs_maior_25_menor_igual_35' => $this->cs_maior_25_menor_igual_35,
        'cs_maior_35_menor_igual_45' => $this->cs_maior_35_menor_igual_45,
        'cs_maior_45' => $this->cs_maior_45,
        'c90_menor_igual_15' => $this->c90_menor_igual_15,
        'c90_maior_15_menor_igual_20' => $this->c90_maior_15_menor_igual_20,
        'c90_maior_20_menor_igual_30' => $this->c90_maior_20_menor_igual_30,
        'c90_maior_30' => $this->c90_maior_30,
    ]);

    $query->andFilterWhere(['>=', 'data', $this->dateStart])
    ->andFilterWhere(['<=', 'data', $this->dateEnd]);

    return $dataProvider;
 }
}

my _search.php:     

use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kartik\widgets\DatePicker;


/* @var $this yii\web\View */
/* @var $model app\models\searches\SlasSearch */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="slas-search">

<?php $form = ActiveForm::begin([
    'action' => ['index'],
    'method' => 'get',
]); ?>

<?= $form->field($model, 'dateStart')->widget(DatePicker::classname(), [
'options' => ['placeholder' => ''],
    'pluginOptions' => [
        'autoclose'=>true,
        'format' => 'yyyy-mm-dd'
    ]
]);?>
<?= $form->field($model, 'dateEnd')->widget(DatePicker::classname(), [
'options' => ['placeholder' => ''],
    'pluginOptions' => [
        'autoclose'=>true,
        'format' => 'yyyy-mm-dd'
    ]
]);?>
<?= Html::submitButton('<i class="glyphicon glyphicon-play"></i>', ['class' 
=> 'btn btn-success pull-right']) ?><br>
<?php ActiveForm::end(); ?>

</div>

我的网址似乎正确:          / web / slas / index?SlasSearch%5BdateStart%5D = 2017-12-01&amp; SlasSearch%5BdateEnd%5D = 2017-12-01

没有错误,只是不过滤,如果我过滤日期只有它工作,只有我尝试过滤之间! 有人可以指出我的错误或我的方法有什么问题。 在此先感谢,Motorui

1 个答案:

答案 0 :(得分:0)

public function rules()
{
return [
    [['id', 'pc_menor_igual_10', 'pc_maior_10_menor_igual_20', 
'pc_maior_20_menor_igual_30', 'pc_maior_30', 'ps_menor_igual_25', 
'ps_maior_25_menor_igual_35', 'ps_maior_35_menor_igual_45', 'ps_maior_45', 
'cc_menor_igual_5', 'cc_maior_5_menor_igual_10', 
'cc_maior_10_menor_igual_20', 'cc_maior_20', 'cs_menor_igual_25', 
'cs_maior_25_menor_igual_35', 'cs_maior_35_menor_igual_45', 'cs_maior_45', 
'c90_menor_igual_15', 'c90_maior_15_menor_igual_20', 
'c90_maior_20_menor_igual_30', 'c90_maior_30'], 'integer'],
    [['data', 'dateStart', 'dateEnd'], 'safe'],
];
}
这个简单!我怎么能看到它!