beforesave() is working in yii2?


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()



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

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

		       -> from('batch as b')

		       -> join('join',

				'course as c',		 	






		foreach($fees_trans as $fee){




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



			$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;



			return true;


can u please suggest solution.


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;