Monday, May 11, 2015

Re: How to set Cake 3.0 to save with different date format ?

Here's a pastebin (paid for it, might as well use it).

It occurred to me there was something i hadn't tried. i've been using an accessor function in the Entity to format the field to m/d/Y for screen. Without it, i get the time (12:00 AM), and the date digits don't have leading 0's. So the accessor was helping solve that. i commented that function and the setter works. The date goes into the SQL statement the way it's expected to (also without the extra format() call, just a Time object).

i've also tried commenting the setter, but end up with the original POST string in my UPDATE statement. So i need the setter.

But i feel i need a getter to format it the way i want for output. i've been using getters to provide the individual formats i need, which feels like function pollution. i guess i could just use the setter to make a Time object & when i access the field, call ->format() on it with the format i need...

Suggestions for a more clever/elegant approach?

Thanks for the help.
-joe


On Monday, 11 May 2015 03:42:42 UTC-4, José Lorenzo wrote:
This line means that you are storing in the entity an string, why do you that? Also, how are you saving the data, can you use gist to show all your code?

return $this->_properties['ready_date']->format('Y-m-d');

On Monday, May 11, 2015 at 7:18:50 AM UTC+2, Joe T. wrote:
i found this topic here and on Stack Overflow: https://stackoverflow.com/questions/29336852/how-to-format-date-fields-before-save-in-cakephp-3
The hash on the linked Doc page is no longer valid.

Like the original post, i don't want to use the default select-based date inputs. i'm formatting my date in US format for output ('m/d/Y') and using the 'mdy' Validator. i've also written a mutator in the Entity to convert the date string to a Time object:
protected function _setReadyDate($readyDate)
{
    $this
->_properties['ready_date'] = new Time($readyDate);
   
return $this->_properties['ready_date']->format('Y-m-d');
}

i added the lines from the Stack answer to my bootstrap.php, but that didn't work either:
I18n::locale('en-US');
Type::build('datetime')->useLocaleParser();

No matter what i do, the MySql UPDATE uses the 'm/d/Y' string instead of the date object:
UPDATE listings SET ready_date = '06/01/2015' , modified = '2015-05-11 05:00:54' WHERE id = 20

What do i try next?

Thanks,
joe t.






On Thursday, 2 April 2015 14:56:29 UTC-4, José Lorenzo wrote:
Read this:

http://book.cakephp.org/3.0/en/orm/database-basics.html#parsing-localized-datetime-data

On Thursday, April 2, 2015 at 6:10:58 PM UTC+2, Celso wrote:
Hi guys I have a table in Mysql with a date field,  and I need to allow my user writes something like 31/12/2015.


echo $this
->Form->input('data_nascimento', ['type' => 'text', 'maxlength'=> '10']);

I've tried
Time::$defaultLocale = 'pt-BR';

in AppController but the problem persists...
and 
'timezone' => 'BRT'
in app.php does not works

If I update the value directly in MySQL and use the
Time::setToStringFormat('dd/MM/YYYY');
in AppController the date is showed in a nice format however my real problem is how to save() because I don't know how change the locale of Cake 3.0.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: