Btw, the code you link to in afterFind is a dead link.

On Jan 21, 3:46 pm, leo <[email protected]> wrote:
> Sorry. I just got the impression you were reinventing the wheel. PHP
> will do the formatting for you.
>
> On Jan 21, 3:26 pm, Ernesto <[email protected]> wrote:
>
> > thx for the resp but that's not my problem. :)
>
> > i'm getting some errors like
> > - data from related models not being formatted
> > - data from related models being formatted many times
>
> > On 20 Gen, 17:18, leo <[email protected]> wrote:
>
> > > I use srttotime() and pass the result to date() using format 'c' in
> > > PHP5 or 'Y-m-d' in PHP4 viz:
> > >     date('Y-m-d', strtotime($date))
> > > $date will need to be formatted like "10 September 2000" - I don't
> > > think the month number will work, but look at the PHP manual for more
> > > info.
>
> > > This code I use in the controller::edit or controller::add functions
> > > like:
> > >                 if (!empty($this->data)) {
> > >                         $this->data['Event']['start_date'] = date
> > > ('c' ,strtotime($this->data['Event']['start_date']));
> > >                 --snip--
>
> > > On Jan 20, 4:25 pm, Ernesto <[email protected]> wrote:
>
> > > > Hello.
>
> > > > i'm writing an app for my little business. i'm using CakePHP + MySQL.
>
> > > > My problem are the DB fields formatted in US way. For example
> > > > - dates in MySQL must be written "YYYY-MM-DD" when we usually use "DD/
> > > > MM/YYYY".
> > > > - US float notation uses dot (.) instead of comma (,) as decimal
> > > > separator.
>
> > > > i wrote some code to resolve that "issue" in both ways (read/save) but
> > > > i'm getting some errors, especially on related models.
>
> > > > @@ here's my DB schema
>
> > > > Person --> HABTM (persons_articles) --> Article --> BELONGSTO -->
> > > > Article_code
>
> > > > @@ here's the error
>
> > > > Article_code contains a "datetime" field (Article_code.Created).
>
> > > > when the page //Cake/persons/details is loaded the
> > > > Article_code.Created gets formatted many times.
>
> > > > @@ here's the incriminated code:
>
> > > > class AppModel extends Model {
>
> > > >         function afterFind($results, $primary = false) {
> > > >                 //the code in this function can be found at
> > > >                 
> > > > //http://groups.google.com/group/cake-php/browse_thread/thread/
> > > > 32ab9f8baed4a05a/062678e92b080f18
> > > >                 //i just renamed the "doAfterFind" function to 
> > > > "stdFormat"
> > > >                 //thx @ the original author of that code.
> > > >         }
>
> > > >         function beforeSave() {
> > > >                 //i convert the data from EU to US so i can save data 
> > > > in database
> > > >                 $this->data = $this->stdFormat($this->data, "ITAtoUS);
> > > >                 return true;
>
> > > >         }
>
> > > >         function stdFormat($data, $format) {
> > > >                 //standard formats, applied to every field of every 
> > > > model.
> > > >                 //this function works on flat arrays (field => value)
> > > >                 foreach ($data as $fieldName => &$value) {
> > > >                         if ($this->hasField($fieldName)) {
> > > >                                 switch 
> > > > ($this->getColumnType($fieldName)) {
> > > >                                         case "datetime":
> > > >                                                 list($date, $time) = 
> > > > explode(" ", $value);
> > > >                                         case "date":
> > > >                                                 switch ($format) {
> > > >                                                         case "UStoITA"
> > > >                                                                 
> > > > list($y, $m, $d) = explode("-", $data);
> > > >                                                                 $value 
> > > > = trim("$d/$m/$y $time");
> > > >                                                         break;
> > > >                                                         case "ITAtoUS":
> > > >                                                                 
> > > > list($g, $m, $a) = explode("/", $data);
> > > >                                                                 $value 
> > > > = trim("$y-$m-$d $time");
> > > >                                                         break;
> > > >                                                 }
> > > >                                         break;
> > > >                                         case "float":
> > > >                                                 switch ($format) {
> > > >                                                         case "UStoITA":
> > > >                                                                 
> > > > //sostituisco il separatore decimale (da punto a virgola)
> > > >                                                                 $value 
> > > > = str_replace(".", ",", $value);
> > > >                                                         break;
> > > >                                                         case "ITAtoUS":
> > > >                                                                 $value 
> > > > = str_replace(",", ".", $value);
> > > >                                                         break;
> > > >                                                 }
> > > >                                         break;
> > > >                                 }
> > > >                         }
> > > >                 }
> > > >                 $data = modelFormat($data, $format);
> > > >                 return $data;
> > > >         }
>
> > > >         function modelFormat($data, $format) {
> > > >                 //placeholder
> > > >                 //this function is designed to be overwritten in models
> > > >                 //this function must contain all the model's formats
> > > >                 return $data;
> > > >         }
>
> > > > }
>
> > > > PS: Any idea on how to clean up/simplify that code is welcome :)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to