Thank you Aleksey, this is a much better approach! It has also forced me into reading up on Zend_Date, and it would appear using it to handle my dates throughout my application will be beneficial too.

Thanks again.

On 30/05/10 23:20, Aleksey Zapparov wrote:
Hello,

To solve such problem I was overriding setters and getters of dates right in
my models, something like this:

class MyModel extends Doctrine_Record
{
     public function setTableDefinition()
     {
         // ...
         $this->hasColumn('some_date', 'date');
         // ...
     }

     public function getSomeDate()
     {
         return new Zend_Date($this->_get('some_date'), 'YYYY-MM-dd');
     }

     public function setSomeDate($value)
     {
         if ($value instanceof Zend_Date) {
             $value = $value->toString('YYYY-MM-dd');
         } else if (! Zend_Date::isDate($value)) {
             throw new App_Exception('Unknown some_date format');
         } else {
             throw new App_Exception('Unsupported type of some_date');
         }

         $this->_set('some_date', $value);
     }
}


2010/5/30 Kris Willis<li...@kriswillis.com>:
Hi there,

I'm currently writing a CRUD system which includes fields for dates. I have
the creation of records working fine, although I am having some issues
working with the dates when retrieving them from the database and setting
the form values with them ready for updating.

The dates are being stored in a date field in the database (YYYY-MM-DD), but
using the English formatting (DD/MM/YYYY) within the user interface.

The problem I am having is that I can retrieve almost all dates fine and
display them in the form fields without issue, but dates where the day and
month digits are the same (i.e. 2010-05-05) refuse to be reformatted (to
05/05/2010).

I'm interfacing with the database via Doctrine. In my controller I'm
currently fetching the record by it's ID field and replacing the dates
within the object with the English formatting using a custom action helper
before passing it to the form for population.

Doctrine Model: http://pastie.org/private/8dsacq6wuxcfdxb88t9s3q
Controller Action: http://pastie.org/private/ois3voinrkra6suc6hvdg
Action Helper: http://pastie.org/private/tlkr7y5taqfyinpyjxlzg
Form: http://pastie.org/private/xhzwnbkfkh5cegqhfjow

After messing around with it for a while, I can manually set the date string
in the object within my controller to be 02/02/2010 for example, which is
passed to the form fine, but if I change it to 03/03/2010 and the original
value in the database is 03/03/2010, the form will display the original
MySQL date of 2010-03-03. I can't figure out why this is happening to this
specific pattern of dates!

I have concocted a workaround which involves setting the dates within the
retrieved object to null, and then setting them to the English formatted
date (http://pastie.org/private/4ojzolzrm7f6vm5ixeqlw), but I'd prefer to
figure out why I am having this issue in the first place and how to resolve
it properly.

Any help is much appreciated.

Regards,
Kris



Environment:
Ubuntu 10.04
Apache 2.2.14
MySQL 5.1.41
PHP 5.3.2
Zend Framework 1.10.3
Doctrine 1.2.2





Reply via email to