Function inside a model in Yii2

i am working in Accounts and i have to add expense. So for that i used the following code inside model




  public function accounts_entry($type,$ledger_type,$amount,$against,$remarks,$entity_name,$posting_date,

    						  		$ref_no,$ref_date,$payment_method,$fiscal_year)

    	{

                $this->account				=	$ledger_type;

    			$this->debit				=	$amount;

    			$this->credit				=	0;

    			$this->against				=	$against;

    			$this->remarks				=	$remarks;

    			$this->entity_name			=	$entity_name;


    			....

    		

                $this->save();

        }

For each entry of expense i have to do two entries into table tabGLEntry(with value in debit and credit fields interchanged). If i try the same code once more then the last value will be saved. How can i save the model two times. I don’t wanna use $model= new Tableglentry ();

In Yii1 i could have used

	`$this->unsetAttributes();` after   `$this->save();` that way i could have saved both entries. Is there any such way in Yii2? (unsetAttributes is not there in Yii2)

Edit:-

i don’t want to use new model object. so i tried like this




 $this->isNewRecord = true;

                if($this->save())

                {

    				$this->setAttributes(['account'=>NULL,'debit' => NULL,'credit'=>NULL,'against' => NULL,

    				'remarks'=>NULL,'entity_name' => NULL,'posting_date'=>NULL,'reference_no' => NULL,'reference_date'=>NULL,

    				'payment_method_id' => NULL,'voucher_category'=>NULL,'fiscal_year' => NULL,'voucher_no'=>NULL,'name' => NULL,]);

    			}

so i thought it will solve my problem but it didn’t. I got an error like this

only my first entry is getting saved other one is not because both are getting same id value(Primary key)

http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#$isNewRecord-detail

But I would make it a static function.




public static function accounts_entry( ... )

{

    $model = new Tableglentry ();


    $model->account = $ledger_type;

    $model->debit = $amount;

    $model->creadit = 0;

    ...

    $model->save();


    $model->newRecord = true;

    $model->debit = 0;

    $model->credit = $amount;

    ...

    $model->save();

}



why so?

In this function we are dealing with 2 rows of a table.

A non-static member function of ActiveRecord should not manipulate multiple rows, because an instance of ActiveRecord is a representation of a single row.