CakePHP //FIELD MONEY (DATABASE, FRONT, BACK)

<?php // DATABASE // your field in database have to // DECIMAL(12, 2) // ENTITY FILE eg.: "Model/Entity/Plan.php" // Accessor // this accessor return formated value and // checking when update not occurred public function _getPrice($val) { if (is_float($val)) // if value from database return Number::format($val, [ 'places' => 2 ]); else // if value defined in your form return $val; } // Mutator // here we will save the value as decimal protected function _setVal($val) { return $this->_toDecimal($val); } // function that help to return the Money formated to decimal // to save in DB private function _toDecimal($value) { $valueWithoutSymbols = str_replace(['.', ','], ['', '.'], $value); $valueFormated = number_format($valueWithoutSymbols, 2, ".", ""); return $valueFormated; } /* if ( You::likeIt() ) return 'http://richellyitalo.com && http://twitter.com/richellyitalo'; */
Here we return the formatted value of the DB, and also do the reverse. Save the formatted value to a decimal default value, as defined in the database.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.