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