Jump to content

Yii2 - How to insert a date with the correct timestamp?


benoit1980

Recommended Posts

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 found1. 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

 

Edited by benoit1980
Link to comment
Share on other sites

  • 1 year later...

Hi benoit1980,
 
First of all, sorry for "late" answer :)
 
It seems that You don't have namespace for ActiveRecord class

use yii\db\ActiveRecord;

If You won't specify this, PHP will look for that class in current namespace, like You see in throwed exception, 'app\models'.

 

The other way is to override ActiveRecord class in 'app\models'

<?php

namespace app\models;

use yii\db\ActiveRecord as BaseActiveRecord;

class ActiveRecord extends BaseActiveRecord
{
 // ...
}

Tip: IDE like NetBeans have auto detecting for namespaces (Ctrl+Shif+I), which can save alot of time :)

Edited by Yupik
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.