Endless loop in afterSave function in yii2 -
i have aftersave function in model, function executes, in endless loop. reason? function inserts data db, in multiple rows. thanks.
my function:
public function aftersave($insert) { $modelproject = projects::find() ->where(['status' => 2]) ->one(); $idproject = $modelproject->pid; $candidateforproject = new candidateforproject(); // $candidateforproject->id = 3; $candidateforproject->idproject = $idproject; $candidateforproject->idcandidate = $this->prid; $candidateforproject->idquestionnaire = $this->id; $candidateforproject->idstatus = 0; $candidateforproject->insert(); $answerone = questionsgrades::findone($this->answer1); $grade1 = $answerone->grade; $answertow = questionsgrades::findone($this->answer2); $grade2 = $answertow->grade; $answerthree = questionsgrades::findone($this->answer3); $grade3 = $answerthree->grade; $answerfour = questionsgrades::findone($this->answer4); $grade4 = $answerfour->grade; $answerfive = questionsgrades::findone($this->answer5); $grade5 = $answerfive->grade; $grade = $grade1 + $grade2 + $grade3 + $grade4 + $grade5; $this->degree=$grade; $this->save(['degree']); } return parent::aftersave($insert); }
i can see using $this->save(); in aftersave action. cause calling aftersave() again. here place loop.
possible fix use $this->update(); instead of $this->save();
but, honestly, think should remaster logic architecture.
Comments
Post a Comment