Yii Framework Forum: How To Use Calculated _Post Value In A Model To Calculate Another Field In The Same Model Before Actioncreate Save It To Db - Yii Framework Forum

Jump to content

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

How To Use Calculated _Post Value In A Model To Calculate Another Field In The Same Model Before Actioncreate Save It To Db how to use calculated _post value in a model to calculate another fiel Rate Topic: -----

#1 User is offline   rynd75 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 12-January 13

Posted 21 January 2013 - 01:13 AM

hello everyone,

i got problem while getting calculated _post value and use it to calculate a new field value, i need to use the calculated first _post Value before it being saved to the db, and use it to calculate another field value and finally save both of them to a table.

here's my code :

my controller code :
public function actionCreate()
	{
		$model=new Stuff;
		$tot = Stuff::model()->getAttributes(array('stuff_total_price'), array('qty'), array('price_per_unit'),array('discount_unit'), array ('price_after_discount'));
		
		//$model=$this->loadModel($id);
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		
		if(isset($_POST['Stuff']))
		{
			
			$model->attributes=$_POST['Stuff'];
			
			if($model->save(
			$model['stuff_total_price'] = ($model['qty'] ) * ($model['price_per_unit'] )
			 &&
			$model['price_after_discount'] = (($model('stuff_total_price') -(($model['discount_unit']) * ($model['stuff_total_price'])))))
				$this->redirect(array('view','id'=>$model->stuff_id));
		}

		$this->render('create',array(
			'model'=>$model, 
		));
	}



here is the condition, i need to calculate
stuff_total_price = qty*price_per_unit
price_after_discount = stuff_total_price - (discount_unit * stuff_total_price)


i know it sounds stupid while i can get price_after_discount using a formula :
price_after_discount = ((qty*price_per_unit) - (discount_unit* (qty*price_per_unit)))

in my case i won't do that, cause i need to use stuff_total_price and price_after_discount separately in another table to calculate another value in the other table, something like monthly report - sum up the stuff_total_price and sum_up the price_after_discount to get a monthly report..

my question is, while i use the code there was no error, but, it comes with the result like this :
stuff_id 46
stuff_name drill
Qty 12
price_per_unit 12
discount_unit 0.10 (in percentge = 10%)

stuff_total_price 0 ---->> the result = 0
price_after_discount 0 ---->> the result = 0

i really need to fix this up as soon as possible, can anyone find out what should i do this things?


thanks in advance>>
regards.
0

#2 User is offline   canailla007 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 06-January 13

Posted 20 July 2013 - 05:19 PM

View Postrynd75, on 21 January 2013 - 01:13 AM, said:

hello everyone,

i got problem while getting calculated _post value and use it to calculate a new field value, i need to use the calculated first _post Value before it being saved to the db, and use it to calculate another field value and finally save both of them to a table.

here's my code :

my controller code :
public function actionCreate()
	{
		$model=new Stuff;
		$tot = Stuff::model()->getAttributes(array('stuff_total_price'), array('qty'), array('price_per_unit'),array('discount_unit'), array ('price_after_discount'));
		
		//$model=$this->loadModel($id);
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		
		if(isset($_POST['Stuff']))
		{
			
			$model->attributes=$_POST['Stuff'];
			
			if($model->save(
			$model['stuff_total_price'] = ($model['qty'] ) * ($model['price_per_unit'] )
			 &&
			$model['price_after_discount'] = (($model('stuff_total_price') -(($model['discount_unit']) * ($model['stuff_total_price'])))))
				$this->redirect(array('view','id'=>$model->stuff_id));
		}

		$this->render('create',array(
			'model'=>$model, 
		));
	}



here is the condition, i need to calculate
stuff_total_price = qty*price_per_unit
price_after_discount = stuff_total_price - (discount_unit * stuff_total_price)


i know it sounds stupid while i can get price_after_discount using a formula :
price_after_discount = ((qty*price_per_unit) - (discount_unit* (qty*price_per_unit)))

in my case i won't do that, cause i need to use stuff_total_price and price_after_discount separately in another table to calculate another value in the other table, something like monthly report - sum up the stuff_total_price and sum_up the price_after_discount to get a monthly report..

my question is, while i use the code there was no error, but, it comes with the result like this :
stuff_id 46
stuff_name drill
Qty 12
price_per_unit 12
discount_unit 0.10 (in percentge = 10%)

stuff_total_price 0 ---->> the result = 0
price_after_discount 0 ---->> the result = 0

i really need to fix this up as soon as possible, can anyone find out what should i do this things?


thanks in advance>>
regards.


Can you solve it?
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