Yii 2.0 Date Input On Form Submition Help

Hi All,

I would like to know if someone could help me with this, I have been struggling for days and cannot find an answer.

I am new to OOP and still struggle a bit with the logic.

I have installed Yii2 on my local machine, all I want is insert a date in the correct format in the database as:

DD/MM/YYYY

I cannot believe how hard it is just ti work this one out as a beginner…

I tried this:

http://www.yiiframework.com/doc-2.0/guide-behaviors.html:

And added this code inmy Posts Model:




use yii\behaviors\TimestampBehavior;


class User extends ActiveRecord

{

    // ...


    public function behaviors()

    {

        return [

            'timestamp' => [

                'class' => TimestampBehavior::className(),

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',

                ],

            ],

        ];

    }

}




I have the corresponding database fields as created_at and updated_at set as INT.

Unfortunately, when I try to save my form I get this error:




PHP Fatal Error – yii\base\ErrorException


Class 'app\models\ActiveRecord' not found

1. in C:\wamp\www\yii2\models\posts.php at line 61

    }


public function behaviors()

    {

        return [

            'timestamp' => [

                'class' => TimestampBehavior::className(),

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',

                ],

            ],

        ];

    }





}




Any idea why please?I have followed the Yii website tutorial correctly but still cannot get the date input to work.

Thank you,

Regards,

Ben

Set your own value to return in the TimeStampBehavior like below:




public function behaviors()

    {

        return [

            'timestamp' => [

                'class' => TimestampBehavior::className(),

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',

                ],

                'value' => function($event) {

                    $format = "d/m/Y"; // any format you wish

                    return date($format); 

                }

            ],

        ];

    }

}



Thanks Kartik,

I added the return and still get this




PHP Fatal Error – yii\base\ErrorException


Class 'app\models\ActiveRecord' not found



Thank you,

Ben

This is a problem in your model class definition (not related to dates at all)… on the top where you are defining your model:

You will have to change this line which you have in your case something like this




class YourModel extends ActiveRecord



to




class YourModel extends \yii\db\ActiveRecord



Ensure your namespaces are correct.

Thanks Kartik,

Yes this is the first thing I checked,hereis my fullcode, this is a new Yii basic app installation:




<?php

namespace app\models;

use Yii;

use yii\behaviors\TimestampBehavior;


/**

 * This is the model class for table "posts".

 *

 * @property integer $id

 * @property string $title

 * @property string $data

 * @property integer $created_at

 * @property integer $updated_at

 */


class posts extends \yii\db\ActiveRecord

{

    

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'posts';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['title', 'data'], 'required'],

            [['data'], 'string'],

            [['created_at', 'updated_at'], 'integer'],

            [['title'], 'string', 'max' => 255]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'title' => 'Title',

            'data' => 'Data',

            'created_at' => 'Created At',

            'updated_at' => 'Updated At',

        ];

    }

    

    

    

    public function behaviors()

    {

        return [

            'timestamp' => [

                'class' => TimestampBehavior::className(),

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',

                ],

                'value' => function($event) {

                    $format = "d/m/Y"; // any format you wish

                    return date($format); 

                }

            ],

        ];

    }

}



The following lines are not namespaced:




'attributes' => [

     ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

     ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',

],



You can use this following line at the top of your class




use yii\db\ActiveRecord;



This should resolve your issue.

Thanks again Kartik,

Yes it worked but I had to had another line(as shown below) and letme explain where I really struggled, I kept having the format in the database as YY/MM/DD and thought…what am I doing wrong???

A programmer has just told me that we cannot store the date as DD/MM/YY in mysql, it has to be the other butinstead we have to reformat the view side of of the date on the output…I had no idea…




public function behaviors()

    {

        return [

            'timestamp' => [

                'class' => TimestampBehavior::className(),

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',

                ],

                'value' => new Expression('NOW()'),

            ],

        ];

    }


}



So I was looking at this page:

http://www.yiiframework.com/doc-2.0/yii-base-formatter.html#$datetimeFormat-detail

But it does not give any great info on what can be added to:




    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'datetimeFormat' => ['d-m-Y H:i:s'],

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'title',

            'data:ntext',

            'created_at',

            'updated_at',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>



to make it look as DD/MM/YYYY

I can see this line that says:

$datetimeFormat public property

string $datetimeFormat = ‘Y-m-d H:i:s’

But how to interpret this please? I am trying to understand the concept… I tried to add it as this:




    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'dateFormat' => ['d-m-Y'],

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'title',

            'data:ntext',

            'created_at',

            'updated_at',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>




But this causes an error:

#Unknown set property.

Thanks,

Ben