开发者

Convert date to string upon saving a doctrine record

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开发者_如何学Python->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?


You should be defining your schema using a yaml file. Using the yaml file, you can generate your classes using the Doctrine command line interface. You can also use the CLI to generate SQL for the DB, and create the DB and the tables.

Doctrine should take care of converting your date to something that's ok for your DB and back again when you load your object.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜