Yii suggests to have created_at and updated_at fields be timestamp and mysql data type to be of integer. I have setup both the columns same way.
Issue is with grid view, I am using kartik\grid\GridView and data is displayed correctly using:
[
'attribute'=>'created_at',
'value' => function ($model, $index, $widget) {
return Yii::$app->formatter->asDateTime($model->created_at);
},
'filterType' => GridView::FILTER_DATE,
'filterWidgetOptions' => [
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'autoclose' => true,
'todayHighlight' => true,
]
],
],
In my model I have:
[['created_at', 'updated_at'], 'integer'],
which I have tried changing to:
[['created_at', 'updated_at'], 'safe'],
Also in model timestamp behavior is set using:
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
In my Search Model I have:
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,
//'created_at' => $this->created_at,
//'updated_at' => $this->updated_at,
]);
// Convert date to Unix timestamp
if (!empty($params['ClickSearch']['created_at'])) {
$query->andFilterWhere([
'date(from_unixtime(created_at))' => date('Y-m-d',strtotime($params['ClickSearch']['created_at']))
]);
}
This is all I have done by reading form lots of stackoverlfow and yii2 forums. Now I am stuck at
if(!$this->validate()) {
This one fails and gives me error that created_at must be an integer.
If I comment out $this->validate() part from above code then it works fine, filter works exactly as expected, but I believe its not good to remove this filter.
Can you guys suggest how to keep this validation and configure column correctly?