beforesave() is working in yii2?

Hi,

in my fees module, i ve used beforesave() in my feestransaction model file.

but its not working.

i want that if course fees=15000 then when i am collecting fees, and if paidfees > 15000 then it will not save fess and give message for that…

for that i ve used beforesave()

here is my code…




public function beforeSave()

	{

		$fees_trans=FeesPaymentTransaction::findAll('fees_payment_student_id='.$_REQUEST['id']);

		$query= new \yii\db\Query();

		$query -> select(['batch_fees'])

		       -> from('batch as b')

		       -> join('join',

				'course as c',		 	

				'b.course_id=c.course_id')

			->where('b.batch_id='.StudentTransaction::findOne($_REQUEST['id'])->student_transaction_batch_id);

		$command=$query->createCommand();

		$total_payable=$command->queryOne();




		$total=0;

		foreach($fees_trans as $fee){

			$total=$total+$fee->fees_payment_amount;

		}

		if($this->isNewRecord){

			$total = $total+$this->fees_payment_amount;

		}

		else{

			$oldfee = $fees_trans=FeesPaymentTransaction::findOne($_REQUEST['fees_id'])->fees_payment_amount;

			$total = ($total-$oldfee)+$this->fees_payment_amount;

		}

		if($total > $total_payable['batch_fees'] ){

			$this->addError('fees_payment_amount','<b style="color:red">You can not take an advance fees for student.</b>');

			return false;

		}

		else

			return true;

	}



can u please suggest solution.

thanks.

beforeSave() public method

This method is called at the beginning of inserting or updating a record.

The default implementation will trigger an EVENT_BEFORE_INSERT event when $insert is true, or an EVENT_BEFORE_UPDATE event if $insert is false. When overriding this method, make sure you call the parent implementation like the following:

public function beforeSave($insert)

{

if (parent::beforeSave(&#036;insert)) {


    // ...custom code here...


    return true;


} else {


    return false;


}

}