Sunday, January 22, 2012

problem with international numbers

Dear all,

I have some problems with converting numbers (especialy digit
seperator) from a german comma seperated number into a number for the
database.

My controller has nothing special but this:
public $helpers = array('Html','Number', 'Form');
-> referres to the NumberHelper in lib/cake/view/helper


My model is:

Class Post extends AppModel {
public $name = 'Post';
public $actsAs = array(
'Date' => array('mydate'),
'Number' => array('num1')
);
}
?>

My Number Behavior is as follow:
<?php
/**
* Number Model Behavior for CakePHP
*
*
* @package behaviors
* @author Christian Schulte
* @version 0.1
* @date 2012/01/22
* @licence MIT
* @repository null
* */
class NumberBehavior extends ModelBehavior {
/**
* Default settings
*
* @var array
* @access protected
*/

protected $_defaults = array(
'fields' => array(),
'options' => array(
//number_format ( float $number , int $decimals = 0 , string
$dec_point = '.' , string $thousands_sep = ',' )
'fromFormat' => array(0,',','.'), // zur
Anzeige in View
'toFormat' => array(0,'.',',') // zum Abspeichern in
DAtenbank
)
);

/**
* Initiate Date Behavior
*
* @param object $model
* @param array $config behavior settings to override
* @return void
* @access public
*/
public function setup(&$model, $config = array())
{
// assign default settings
$this->settings[$model->alias] = $this->_defaults;

foreach ($config as $key => $value) {
if (is_array($value)) {
$fieldName = $key;
// merge default options with new options if defined
$options = $value + $this->settings[$model->alias]
['options'];

}
else {
$fieldName = $value;
$options = $this->settings[$model->alias]['options'];
}
if ($model->hasField($fieldName)) {
$this->settings[$model->alias]['fields'][] =
array('fieldName' => $fieldName) + $options;
}
}
}

/**
* Before save callback
*
* @param object $model Model using this behavior
* @return boolean True and contine to save action
* @access public
*/

public function beforeSave(&$model)
{
foreach ($this->settings[$model->alias]['fields'] as $field) {
if (isset($model->data[$model->alias]
[$field['fieldName']])) {
$toFormat = $field['toFormat'];
$number = $model->data[$model->alias][$field['fieldName']];
// $number = number_format ( $number , $fromFormat[0] ,
$fromFormat[1], $fromFormat[2] );
//number_format ( $number , $toFormat[0] , $toFormat[1],
$toFormat[2] );
$newNumber = number_format ( $number , $toFormat[0] ,
$toFormat[1],$toFormat[2] );
if ($newNumber) {
$model->data[$model->alias][$field['fieldName']] =
$number;
}
}
}
return true;
}

/**
* After find callback
*
* @param object $model Model using this behavior
* @param array $results from the Model after find operation
* @param boolean $primary indicates wheter or not the Model is an
originated/association Model
* @return array modified results
* @access public
*/
public function afterFind(&$model, $results, $primary)
{
foreach ($results as $key => $value) {
foreach ($this->settings[$model->alias]['fields'] as
$field) {
if (isset($value[$model->alias][$field['fieldName']]))
{
$fromFormat = $field['fromFormat'];
$newNumber = number_format ( $results[$key][$model-
>alias][$field['fieldName']] , $fromFormat[0] , $fromFormat[1],
$fromFormat[2] );
if ($newNumber) {
$results[$key][$model->alias]
[$field['fieldName']] = $newNumber;
}
}
}
}
return $results;
}

}

My View is as follow:

<!-- File: /app/View/Posts/add.ctp -->

<?php echo $this->Form->create('Post', array('action' => 'edit')); ?>

<table class="main">

<tr>

<td class="left">

<?php

echo $this->Form->input('role',array('label' => 'Rolle'));

echo $this->Form->input('mydate', array('type'=>'text','label' => 'My
Date'));
echo $this->Form->input('num1', array('type'=>'text','label' =>
'Num1'));


/* echo $this->Form->input('num1', array('type'=>'text','label' =>
'Num1',
'value'=>$this->Number->format($this->request-
>data['Post']['num1'], array(
'places' => 2,
'before' => '',
'escape' => false,
'decimals' => ',',
'thousands' => '.'
)
),
));


*/

?>

</td>

</tr>

<tr>

<td colspan="2" class="bottom">

<?php echo $this->Form->submit(__('Submit', true), array('name' =>
'Auftrag speichern', 'div' => false)); ?>

<input type="button" name="Cancel" value="Cancel"
onclick="window.location = '../index' " />

<?php echo $this->Form->end();?>

</td>

</tr>

</table>

As you might see, I've tried different thinks like

with this code I'm in the situation that the number is saved as
rounded: i.e. 34,56 gets 35.00 in the Database, and afterwards
displayed as 35,00

Realy strange,

Hopefully anyone out there has an idea on this.

Thanks in advance,

Christian

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: