I tried the following yaml code:
columns:
created_time:
type: timestamp
notnull: true
default: default CURRENT_TIMESTAMP
In
Notice that DEFAULT CURRENT_TIMESTAMP
does not work the same as Timestampable, and thus you cannot blindly exchange one for the other.
First and foremost, the former uses the date/time of the DB server, while the latter uses a Doctrine magic that calls PHP's date() function on your webserver. In other words, they are two distinct ways of getting the date/time, from two entirely different clock sources. You may be on big trouble if you use Timestampable, your web server runs on a different machine than your DB server, and you don't keep your clocks in sync using e.g. NTP.
Also, the DEFAULT CURRENT_TIMESTAMP
being on the table definition makes for a much more consistent database model IMHO, as no matter how you insert the data (for instance, running INSERT
s on the DB engine command line), you'll always get the current date/time on the column.
BTW, I'm also looking for an answer to the CURRENT_TIMESTAMP
problem mentioned in the initial question, as this is (due to the reasons outlined above) my preferred way of keeping "timestamp" columns.
/**
* @var int
* @ORM\Column(type="datetime", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
*/
protected $created;
after run ./vendor/bin/doctrine-module orm:schema-tool:update --force
Updating database schema... Database schema updated successfully! "1" queries were executed
and run ./vendor/bin/doctrine-module orm:validate-schema
[Mapping] OK - The mapping files are correct. [Database] FAIL - The database schema is not in sync with the current mapping file.
But FAIL for sync appear
Sorry for necroposting. But i have encoutered the same problem. There is solution for doctrine 2 and postgreSql. I have used Gemdo extension and added following strings:
$evm = new \Doctrine\Common\EventManager();
$timestampableListener = new \Gedmo\Timestampable\TimestampableListener;
$timestampableListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($timestampableListener);
YAML:
created:
type: date
options:
default: 0
nullable: true
gedmo:
timestampable:
on: create
updated:
type: datetime
options:
default: 0
nullable: true
gedmo:
timestampable:
on: update
dump-sql:
ALTER TABLE users ADD created DATE DEFAULT CURRENT_DATE NOT NULL;
ALTER TABLE users ADD updated TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL;
If you are willing to sacrifice some portability (see description of columnDefinition attribute) for the ability to use MySQL's automatic initialization TIMESTAMP (see MySQL timestamp initialization), then you can use the following:
Yaml:
created_time:
type: datetime
columnDefinition: TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Annotation:
@ORM\Column(type="datetime", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
You could use the 'Timestampable' functionality in doctrine, eg:
actAs:
Timestampable:
created:
name: created_time
updated:
disabled: true
columns:
created_time:
type: timestamp
notnull: true
You can use:
default: '<?php echo date('Y-m-d H:i:s') ?>'