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,



                $this->account				=	$ledger_type;

    			$this->debit				=	$amount;

    			$this->credit				=	0;

    			$this->against				=	$against;

    			$this->remarks				=	$remarks;

    			$this->entity_name			=	$entity_name;





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)


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

 $this->isNewRecord = true;



    				$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)$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->newRecord = true;

    $model->debit = 0;

    $model->credit = $amount;




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.