views:

443

answers:

2

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?

A: 

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.

Chris Williams
Maybe Doctrine is the wrong tool for the job then. My only other experience with an ORM is hibernate and I was seeking something similar for this project. Basically, I want PHP object corresponding to records in the database, but I also want the ability to add my own methods to these objects. Generating my classes from yaml doesn't allow me to do this. I don't really see how a Doctrine_Record object is any better than an associative array if it doesn't allow me to also define actions on the object.
takteek
You'll still have the ability to add custom code to each object. Basically, Doctrine will create "Base" classes for you. So if you have a User object defined in your yaml file, Doctrine creates: <where ever you specify your models go>/generated/BaseUser.php and <where ever you specify your models go>/User.php. User extends BaseUser. You're free to add to User.php and BaseUser.php will be the only thing changed if/when you change User in your yaml file.
Chris Williams
A: 

Hi, I have a table with a column DOB defined as a date in doctrine (using schema.yml).

When I try to add the data from a form, with a null date, I get a doctrine/form error:

"Warning: strtotime() expects parameter 1 to be string, array given in /var/lib/vendor/symfony-1.4.4/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php on line 1537"

When I print out the data values for the fields,I get different results for timestamp and date data types.

For example:

array 'type' => string 'timestamp' (length=9) 'OLD' => null 'NEW' => string '2010-06-11 10:59:41' (length=19)

array 'type' => string 'date' (length=4) 'OLD' => null 'NEW' => array 'month' => string '2' (length=1) 'day' => string '6' (length=1) 'year' => string '2005' (length=4)

I am going to change the data types and see if I get any different result.

TO me this looks like a bug, but I am probably doing something wrong.

Stay well, Barry

Barry Steele