Yii Framework Forum: Multiplication In Crud - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Multiplication In Crud Rate Topic: -----

#1 User is offline   ghastlyy16 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 09-October 11

Posted 01 November 2012 - 02:33 AM

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'.
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

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.
0

#3 User is offline   ghastlyy16 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 09-October 11

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
0

#4 User is offline   ghastlyy16 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 09-October 11

Posted 01 November 2012 - 03:34 AM

I realize my mistake this will work

$model->total = $_POST['Receipt']['quantity'] * $_POST['Receipt']['price'];
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users