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

[Bug]: Every time you save a version is created, even though no changes have been made to the data. #16997

Open
sevarozh opened this issue Apr 29, 2024 · 6 comments

Comments

@sevarozh
Copy link

Pimcore version

11.1.5 - 11.2.3

Steps to reproduce

  • open an object
  • open Versions tab
  • press 'save and publish' several times without changing any data

https://demo.pimcore.com/admin/login/deeplink?object_260_object
Bildschirmfoto 2024-04-29 um 11 08 58

Actual Behavior

Each time a new version is created with the same data set

Expected Behavior

No new version should be created if no data has changed

@sevarozh sevarozh added the Bug label Apr 29, 2024
@Fahl-Design
Copy link
Contributor

We added a workaround by extending the object save method to dispatch an event (in a try catch block) before the parent save method is called. Then a a listener to check if anything is changed and throw a "skip version save exception" and just skip save at all

@wisconaut wisconaut modified the milestone: 11.3.0 May 2, 2024
@fashxp fashxp added Improvement and removed Bug labels May 2, 2024
@fashxp
Copy link
Member

fashxp commented May 2, 2024

Right now, this is by design. We need to think about if and how we can change that.

@jdreesen
Copy link
Contributor

jdreesen commented May 2, 2024

Yes please. It makes no sense to create a new version when nothing changed.

@Fahl-Design
Copy link
Contributor

It gets even worse when https://pimcore.com/docs/platform/Pimcore/Tools_and_Features/Versioning#settings when "Store version history for X steps" is enabled..

@Cruiser13
Copy link
Contributor

@Fahl-Design would you share your event listener solution or is it tied to specific fields and classes?

@Fahl-Design
Copy link
Contributor

@Cruiser13 it's a mix of both. Some fields can just be checked with a look into "getDirtyFields" others (relation fields) musst be compared against the previos version. Since we use a lot of CLI and workflows. It was a pain and is to specific to share.

in general:

  • we extended every dataobject
  • added a trait
  • and an Event Listener that checks object against (in our case) last published version (published state, image galleries, properties, tasks, version data... )
trait TriggerPreSaveEventOnSaveTrait
{
    public function save(array $parameters = []): static
    {
        try {
            $eventData = new DataObjectEvent($this);
            Pimcore::getEventDispatcher()->dispatch($eventData, ObjectSaveEvent::OBJECT_PRE_SAVE);
        } catch (\OegBundle\Exception\ElementVersionSavedSkippedException $e) {
            // valid case: skipped save to prevent useless versions

            // hacky trick - disable output message in other wrapped processes
            $requestStack = Pimcore::getContainer()?->get('request_stack');
            if ($requestStack instanceof RequestStack) {
                if (RouteHelper::staticIsDataObjectWorkflowTransitionContext($requestStack)) {
                    // needed - otherwise publishing via workflow not working correct!
                    return parent::save($parameters);
                }

                if (RouteHelper::staticIsDataObjectGridBatchContext($requestStack)) {
                    // batch in grid avoid any error output
                    return $this;
                }
            }

            if (PHP_SAPI !== 'cli') {
                // do not run in cli / worker etc, sanity check will fail
                throw new Pimcore\Model\Element\ValidationException('No Change was made - save was skipped');
            }

            // return $this;
        } catch (ElementVersionSavedException $e) {
            // valid case: we saved as "version"
            return $this;
        }

        return parent::save($parameters);
    }
}

(this may change in pimcore 11.2 or 11.3, but we stuck at 11.1 because every time a new "minor" is out, the list of remarkable crazy issues gets longer with issues like: (random pick) #16965

☮️

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

No branches or pull requests

6 participants