user10404360
user10404360

Reputation:

Why Yii2 dateTime() functionality doens't writes the time?

I have Generated CRUD with Gii, but my migrations don't work as expected to.

'id' => $this->primaryKey(),
'student' => $this->string(255)->notNull(),
'diploma_teacher' => $this->string(255)->notNull(),
'type_of_work' => $this->string(255)->notNull(),
'student_classification' => $this->string(255)->notNull(),
'title' => $this->string(255)->notNull(),
'created_at' => $this->dateTime()->notNull(),
'updated_at' => $this->dateTime()->notNull(),

When creating new Diploma Work the field for created_at and updated_at

0000-00-00 00:00:00
0000-00-00 00:00:00

Upvotes: 0

Views: 187

Answers (2)

Chun
Chun

Reputation: 487

I would suggest you declare the create_at and update_at as follow,

'create_at' => $this->dateTime()->notNull()->defaultExpression('now()'),
'update_at' => $this->dateTime()->notNull()->defaultExpression('now()')->append('ON UPDATE CURRENT_TIMESTAMP')

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

You should add the default value expression

'created' => $this->dateTime()->notNull()->defaultExpression('now()')

Upvotes: 0

Related Questions