php - Doctrine2 default timestamp wtih "0000-00-00 00:00:00" format? -
i using doctrine2 orm. have order table.
name | type | attributes | null | default -------------------------------------------------------------------------------- order_id | int | | no | none date_created | timestamp | | no | current_timestamp date_edited | timestamp | on update current_timestamp | no | current_timestamp date_sent | timestamp | | no | 0000-00-00 00:00:00 date_cancelled | timestamp | | no | 0000-00-00 00:00:00 requested_ship_date | timestamp | | no | 0000-00-00 00:00:00 i have added timestampable doctrine extension use date_created , date_edited fields so:
/** * @var \datetime * * @gedmo\timestampable(on="create") * @orm\column(type="datetime") */ private $date_created; /** * @var \datetime * * @gedmo\timestampable(on="update") * @orm\column(type="datetime") */ private $date_edited; however, i'm not sure date_sent , date_cancelled. want them both default 0000-00-00 00:00:00 instead of current time. saw this workaround using timestamps in doctrine2 without plugin, don't want default current timestamp these. reason being, users can 'save' order not 'send' someone. also, don't want date_cancelled current timestamp.
note: don't want store nulls in database, please don't suggest that.
thanks in advance.
update
i have 3 remaining fields in order entity.
/** * @var \datetime * * @orm\column(type="datetime", options={"default":"0000-00-00 00:00:00"}) * @orm\version */ private $date_sent; /** * @var \datetime * * @orm\column(type="datetime", options={"default":"0000-00-00 00:00:00"}) * @orm\version */ private $date_cancelled; /** * @var \datetime * * @orm\column(type="datetime", options={"default":"0000-00-00 00:00:00"}) * @orm\version */ private $requested_ship_date; for reason, when try update requested_ship_date field, ends being "0000-00-00 00:00:00". leave date_sent , date_cancelled fields null , update my local time, not utc time. however, date_created , date_edited fields updated current time in utc time (how want it).
basically, when i'm saving order looks first row, want second row (assuming user requested ship date of 2015-06-08).
order_id | date_created | date_edited | date_sent | date_cancelled | requested_ship_date ----------------------------------------------------------------------------------------------------------------------- 1 | 2015-06-05 12:00:00 | 2015-06-05 12:00:00 | 2015-06-05 07:00:00 | 2015-06-05 07:00:00 | 0000-00-00 00:00:00 1 | 2015-06-05 12:00:00 | 2015-06-05 12:00:00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 2015-06-08 00:00:00 i have set timezone in index.php file date_default_timezone_set('zulu');. don't reset timezone anywhere else.
okay, found crazy workaround this, couldn't find better solution. so, else stuck in same mess am, you're welcome lol.
first, rules:
date_created= updated on creationdate_edited= updated on every updatedate_sent= updated when user clicks "submit" instead of "save"date_cancelled= updated when user decides cancel orderrequested_ship_date= required when creating new order, date customer wants order shipped by.
some code order entity.
/** * @var \datetime * * @orm\column(type="datetime") * @gedmo\timestampable(on="create") */ private $date_created; /** * @var \datetime * * @orm\column(type="datetime") * @gedmo\timestampable(on="update") */ private $date_edited; /** * @var \datetime * * @orm\column(type="datetime", options={"default":"0000-00-00 00:00:00"}) */ private $date_sent; /** * @var \datetime * * @orm\column(type="datetime", options={"default":"0000-00-00 00:00:00"}) */ private $date_cancelled; /** * @var \datetime * * @orm\column(type="datetime") * @gedmo\timestampable(on="change", field="requested_ship_date") */ private $requested_ship_date; /* * constructor */ public function __construct() { if (!$this->date_sent) $this->date_sent = new datetime("0000-00-00 00:00:00", new datetimezone("zulu")); if (!$this->date_cancelled) $this->date_cancelled = new datetime("0000-00-00 00:00:00", new datetimezone("zulu")); } /** * updates time sent * * @return order */ public function send() { $this->date_sent = new datetime("now"); return $this; } /** * updates time cancelled * * @return order */ public function cancel() { $this->date_cancelled = new datetime("now"); return $this; } explanation:
i did not change of how set database columns in order table. tried using @gedmo\timestampable plugin date_sent , date_cancelled fields, couldn't work. noticed timezones off, weird reason, manually set them in constructor. important not automatically set it, or override values set. reason unknown me, couldn't requested_ship_date field working without using @gedmo\timestampable plugin. if took out, every set tried use doctrines type="datetime" result in column equaling 0000-00-00 00:00:00.
if has questions, let me know. had poke @ things awhile until figured out. date_sent , date_cancelled fields worked correctly, , in right timezone when updated them.
Comments
Post a Comment