Skip to content

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

@strtob

Description

@strtob

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions