2017-12-08 4 views
2

3か月前、6か月前、1年前などの日付列をフィルタリングしたいですか? 以下のように、gridviewの検索フィールドにドロップダウンを作成しました。yii2で日付フィールドをフィルタリングする方法

[ 
       'attribute' => 'modified', 
       'value' => 'name', 
       'filter'=>array("ID1"=>"Before Three months","ID2"=>"Before six months","ID"=>"Before Twelve months",), 
      ], 

とmodelsearchに私は次のように検索したい...

if (($this->modified)=="ID1"){ 
    $query->andFilterWhere(['between', $this->modified, 'today', '3monthsago']); 
    } 

が、私は今日の3monthsagoの代わりに、そこにあるべきかを理解できないのですか? クエリでこの変数を計算して渡す方法は?

答えて

0

あなたmodelsearch

$today = date('Y-m-d H:i:s',time()); 
if (($this->modified)=="ID1"){ 
    $endDate = date('Y-m-d H:i:s', strtotime('-3 months')); 
    $query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]); 
}elseif (($this->modified)=="ID2") { 
    $endDate = date('Y-m-d H:i:s', strtotime('-6 months')); 
    $query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]); 
}elseif (($this->modified)=="ID") { 
    $endDate = date('Y-m-d H:i:s', strtotime('-12 months')); 
    $query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]); 
} 
+0

整合性制約違反にこれを追加 - Yiiの\ DB \ IntegrityException SQLSTATE [23000]:整合性制約違反:句はSQLが実行されて 曖昧でどこで1052列 '修正' を「連絡先」からSELECT COUNT(*)LEFT JOIN「会社」の「連絡先」.company_id =会社名。「社内」「国」「連絡先」「国家」=「国」。 'country_code' WHERE( 'modified' =' ID2 ')AND(' ID2' BETWEEN' 2017-12-08 06:10:54 'AND '08/06/2017') – Goli

+0

私は答えを更新しました。 。 –

+0

まだ同じエラーがあります – Goli

関連する問題