|
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 |
|
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 <[hidden email]>: > 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 > > -- Sincerely yours, Aleksey V. Zapparov A.K.A. ixti FSF Member #7118 Mobile Phone: +34 617 179 344 Homepage: http://www.ixti.ru JID: [hidden email] *Origin: Happy Hacking! |
|
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<[hidden email]>: >> 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 >> >> > > > |
|
Hello,
As I told you privately (when I pressed wrong button again :)) first sample is full of bugs. So the correct version of date field muttator should looks like: http://gist.github.com/421508 Also I have spent some time and found more complex and clean way to achieve this and stay DRY :)) Simples version of this can be achieved with help of Doctrine_Record_Filter: http://gist.github.com/422331 And finally the complex one: http://gist.github.com/422387 With complex version you receive ability to do something like this: $person = new Person; $person->birthday = new Zend_Date('01.01.1982', 'dd-MM-YYYY'); $person->birthday->addMonth(10); $person->save(); And as a cover letter you can read my blog post which is some kind of mind/idea flow from first to last: http://blog.ixti.ru/?p=407 Hope this will be helpfull for somebody :)) 2010/5/31 Kris Willis <[hidden email]>: > 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<[hidden email]>: >>> >>> 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 >>> >>> >> >> >> > -- Sincerely yours, Aleksey V. Zapparov A.K.A. ixti FSF Member #7118 Mobile Phone: +34 617 179 344 Homepage: http://www.ixti.ru JID: [hidden email] *Origin: Happy Hacking! |
|
Thank you for your input Aleksey, very much appreciated :)
On 02/06/10 17:22, Aleksey Zapparov wrote: > Hello, > > As I told you privately (when I pressed wrong button again :)) first sample is > full of bugs. So the correct version of date field muttator should looks like: > http://gist.github.com/421508 > > Also I have spent some time and found more complex and clean way to > achieve this and stay DRY :)) Simples version of this can be achieved with > help of Doctrine_Record_Filter: http://gist.github.com/422331 > > And finally the complex one: http://gist.github.com/422387 > > With complex version you receive ability to do something like this: > > $person = new Person; > $person->birthday = new Zend_Date('01.01.1982', 'dd-MM-YYYY'); > $person->birthday->addMonth(10); > $person->save(); > > And as a cover letter you can read my blog post which is some kind of > mind/idea flow from first to last: http://blog.ixti.ru/?p=407 > > Hope this will be helpfull for somebody :)) > > > 2010/5/31 Kris Willis<[hidden email]>: >> 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<[hidden email]>: >>>> >>>> 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 >>>> >>>> >>> >>> >>> >> > > > |
|
Hello,
It's me again :)) Once I started to implement proposed variant on one of my projects I found some things that were boring me :)) So after small investigation I found a better solution: http://blog.ixti.ru/?p=417 So with this solution you don't need App_Record class at all. All you need is to attach filter to the model you want to be able to work with date fields as with Zend_Date objects. 2010/6/2 Kris Willis <[hidden email]>: > Thank you for your input Aleksey, very much appreciated :) > > On 02/06/10 17:22, Aleksey Zapparov wrote: >> >> Hello, >> >> As I told you privately (when I pressed wrong button again :)) first >> sample is >> full of bugs. So the correct version of date field muttator should looks >> like: >> http://gist.github.com/421508 >> >> Also I have spent some time and found more complex and clean way to >> achieve this and stay DRY :)) Simples version of this can be achieved with >> help of Doctrine_Record_Filter: http://gist.github.com/422331 >> >> And finally the complex one: http://gist.github.com/422387 >> >> With complex version you receive ability to do something like this: >> >> $person = new Person; >> $person->birthday = new Zend_Date('01.01.1982', 'dd-MM-YYYY'); >> $person->birthday->addMonth(10); >> $person->save(); >> >> And as a cover letter you can read my blog post which is some kind of >> mind/idea flow from first to last: http://blog.ixti.ru/?p=407 >> >> Hope this will be helpfull for somebody :)) >> >> >> 2010/5/31 Kris Willis<[hidden email]>: >>> >>> 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<[hidden email]>: >>>>> >>>>> 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 >>>>> >>>>> >>>> >>>> >>>> >>> >> >> >> > -- Sincerely yours, Aleksey V. Zapparov A.K.A. ixti FSF Member #7118 Mobile Phone: +34 617 179 344 Homepage: http://www.ixti.ru JID: [hidden email] *Origin: Happy Hacking! |
| Powered by Nabble | Edit this page |
