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

dontVersionFields does not seem to work properly on appended attributes #85

Open
dyuk1987 opened this issue Jun 18, 2021 · 2 comments
Open

Comments

@dyuk1987
Copy link

I have in my model a number of appended attributes, and I have also added them into the dontVersionFields array. However, it didn't seem to work at all.

@chrisrickard
Copy link

Agree, this is causing issues on my end too

@chrisrickard
Copy link

I found a lame-ish workaround (cc @dyuk1987)

Temporarily disable the appends before versioning, and then put back post version.

class Example extends Model
{
    use VersionableTrait {
        VersionableTrait::versionablePreSave as traitVersionablePreSave;
        VersionableTrait::versionablePostSave as traitVersionablePostSave;
    }

    protected $appends = [
      'exampleAppend',
      'anotherExample'
    ];

    // used to temporarily store $appends
    protected $originalAppends = null;

    
    /**
     * Versionable trait post model save.
     *
     * Temporarily remove appends before versioning as we don't want to version the append content,
     * and dontVersionFields does not seem to work properly on appended attributes
     * see https://github.com/mpociot/versionable/issues/85
     *
     * @return void
     */
    protected function versionablePreSave()
    {
        // keep track of original appends
        $this->originalAppends = $this->appends;

        // clear appends so we don't version ammend values
        $this->setAppends([]);

        // call original pre-save trait to the before versioning stuff
        $this->traitVersionablePreSave();
    }

    /**
     * Versionable trait post model save
     *
     * @return void
     */
    protected function versionablePostSave()
    {
        // call original post save trait to create new version
        $this->traitVersionablePostSave();

        // set the original appends back after versioning (as it was disabled in versionablePreSave)
        $this->setAppends($this->originalAppends);
    }
}

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