I created a table name Receipt with columns product, quantity, price, total.
I use crud. In my create new receipt I want the value of 'total' will come from the product of 'quantity' and 'price'.
Page 1 of 1
Multiplication In Crud
#2
Posted 01 November 2012 - 02:41 AM
You don't say whether you want in the form itself or just when the model is saved?
In the first case, you can do it with jQuery on change event of the 'quantity' and 'price' fields. In the second case, you have to set $model->total in your controller's create method (after the setAttributes part), and in that case you may just remove 'total' from the model's 'safe' array.
In the first case, you can do it with jQuery on change event of the 'quantity' and 'price' fields. In the second case, you have to set $model->total in your controller's create method (after the setAttributes part), and in that case you may just remove 'total' from the model's 'safe' array.
#3
Posted 01 November 2012 - 03:01 AM
where is setAttributes part? in the controller? I want to do this when the model is saved. What I did is this
public function actionCreate()
{
$model=new Receipt;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Receipt']))
{
$model->total=$_POST['price'] * $_POST['quantity'];
$model->attributes=$_POST['receipt'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
but this doesnt work
public function actionCreate()
{
$model=new Receipt;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Receipt']))
{
$model->total=$_POST['price'] * $_POST['quantity'];
$model->attributes=$_POST['receipt'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
but this doesnt work
#4
Posted 01 November 2012 - 03:34 AM
I realize my mistake this will work
$model->total = $_POST['Receipt']['quantity'] * $_POST['Receipt']['price'];
$model->total = $_POST['Receipt']['quantity'] * $_POST['Receipt']['price'];
Share this topic:
Page 1 of 1

Help












