Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YII migrations and by default values for table columns

public function up(){

        $this->createTable('POST', array(
            'id' => 'pk',
            'isremoved' => 'integer NOT NULL',
            'removaldate' => 'timestamp NULL',
            'post' => 'text NOT NULL',
            'creationdate' => 'timestamp NOT NULL',
        ));
}

This is the up function for migration. As u see it is query for creating new table. By default YII creates default value for timestamp column equal to CURRENT_TIMESTAMP and crates additional parameter and sets it equal to ON UPDATE CURRENT_TIMESTAMP.

I do not need current value for timestamp and i do not need to update this column on updating row. What i must to do? By the way, u use MySQL

like image 850
Factory Girl Avatar asked May 14 '13 08:05

Factory Girl


2 Answers

For google users like me: Yii2 now has it out of the box.

Now you can add ->defaultExpression('CURRENT_TIMESTAMP') to your definition.

like image 179
leitasat Avatar answered Nov 07 '22 11:11

leitasat


Leverage off the MySQL Create Table Script:

show create table tablename

Which gives:

CREATE TABLE `tablename` (
....
....
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
...

Now Add that to the migration:

'timestamp' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'
like image 1
tread Avatar answered Nov 07 '22 11:11

tread