Filter for created_by in gridview

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?

Remove safe and integer validation for created_at and updated_at field in rules.

use this instead




[['created_at', 'updated_at'], 'date', 'format'=>'dd-MM-yyyy', 'message'=>'{attribute} must be DD/MM/YYYY format.'             ],

in FilterWhere use this




$query

->andFilterWhere(['like', "(date_format( FROM_UNIXTIME(`created_at` ), '%d-%m-%Y %h:%i:%s %p' ))", $this->created_at])

->andFilterWhere(['like', "(date_format( FROM_UNIXTIME(`updated_at` ), '%d-%m-%Y %h:%i:%s %p' ))", $this->updated_at]);



Thx, works.