Convert date to string upon saving a doctrine record
Posted
by takteek
on Stack Overflow
See other posts from Stack Overflow
or by takteek
Published on 2009-11-05T22:42:07Z
Indexed on
2010/06/11
1:13 UTC
Read the original article
Hit count: 273
Hi, I'm trying to migrate one of my PHP projects to Doctrine. I've never used it before so there are a few things I don't understand. In my current code, I have a class similar to this:
class ScheduleItem { private Date start; //A PEAR Date object. private Date end; public function getStart() { return $this->start; } public function setStart($val) { $this->start = $val; } public function getEnd() { return $this->end; } public function setEnd($val) { $this->end= $val; } }
I have a ScheduleItemDAO
class with methods like save()
, getByID()
, etc. When loading from and saving to the database, the DAO class converts the Date
objects to and from strings so they can be stored in a timestamp field.
In my attempt to move to Doctrine, I created a new class like this:
class ScheduleItem extends Doctrine_Record { public function setTableDefinition() { $this->hasColumn('start', 'timestamp'); $this->hasColumn('end', 'timestamp'); } }
I had hoped I would be able to use Date
objects for the start and end times, and have them converted to strings when they are saved to the database. How can I accomplish this?
© Stack Overflow or respective owner