Comparing
#7 with
#8
Content
[...]
We usually assign the received user input (=POST variables) to the model before performing other operations (calculations, validaton, saving, etc.).<br>
Therefore we sugges to do the 'unformatting' during the assignment process. This way, after one of the following lines is executed:
```php
$product->attributes = $_POST['Product']; // or ...
$product->setAttribute('price', $_POST['Product']['price']); // or ...
$product->setAttributes(array('price'=>$_POST['Product']['price']));
```
`$product->price` will be a valid numerical value and available for further use.[...]
###2. The 'unformatting' of a number
Extend the CActiveRecord class and override its functions `setAttribute()` and `setAttributes()` to add the 'unformatting' functionality. <br>
For this, create the new file `components/ActiveRecord.php` with the following content:[...]
}
}
public function setAttribute($name,$value) {
$column = $this->getTableSchema()->getColumn($name); // new
if (stripos($column->dbType, 'decimal') !== false) // new
$value = Yii::app()->format->unformatNumber($value); // new
if(property_exists($this,$name))
$this->$name=$value;
else if(isset($this->getMetaData()->columns[$name]))
$this->_attributes[$name]=$value;
else
return false;
return true;
}
}
```
Derive all models from this new ActiveRecord class. For example in the Product model `models/Product.php` edit the following line:
```php[...]