Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AR Method afterSave() of related model will be marked as deleted #66

Open
strtob opened this issue May 30, 2021 · 1 comment
Open

AR Method afterSave() of related model will be marked as deleted #66

strtob opened this issue May 30, 2021 · 1 comment

Comments

@strtob
Copy link

strtob commented May 30, 2021

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

@optmsp
Copy link

optmsp commented Jan 2, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants