Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define current timestamp in yaml with doctrine?

I tried the following yaml code:

columns:
  created_time:
    type: timestamp
    notnull: true
    default: default CURRENT_TIMESTAMP

In the outputted sql statement, the field is treated as datetime instead of timestamp, which I cannot define the current timestamp in it...

If I insist to use timestamp to store current time, how to do so in yaml?

like image 637
Capitaine Avatar asked May 30 '10 12:05

Capitaine


3 Answers

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")
like image 136
Daniel Criconet Avatar answered Nov 08 '22 06:11

Daniel Criconet


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

like image 42
Flávio Avatar answered Nov 08 '22 05:11

Flávio


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
like image 11
Twelve47 Avatar answered Nov 08 '22 05:11

Twelve47