yii2-relation-trait
yii2-relation-trait copied to clipboard
AR Method afterSave() of related model will be marked as deleted
Hi Guys,
I use this great extension for different projects, thanks a lot!!!
In my case, I would like to write the history of changes in Company Name in a has-many table calles Company Names. For this I use the following method in the company model:
public function afterSave($insert, $changedAttributes) {
// init superclass
parent::afterSave($insert, $changedAttributes);
if(!$insert && isset($changedAttributes['name'])) {
// your code here like $changedAttributes['myField'];
$modelCompanyName = new \app\models\CompanyName();
$modelCompanyName->tbl_company_id = $this->id;
$modelCompanyName->name = $this->name;
$modelCompanyName->valid_from = date('Y-m-d H:i:s');
$modelCompanyName->save();
}
}
Unfortunately, if I save the company with changed name field, the data will be saved in company name table correctly, but will be marked (I use softdelete) as deleted.
I suggest that afterSave is executed after saving the company model, the relation trait will deal after with the related models and use the information of the request, which haven't that information.
One solution will be to use saveAll method with parameters (['companyName']) to avoid saving this information, but if some of the names should be deleted this information will be ignored.
Can I change the order how relation trait save the models or is there any other turnaround?
Thanks a lot!
Tobi
Hey @strtob , curious why you don't use one of the "change audit" modules for yii for this? Or does your User need access to this data?