doctrine-extensions / DoctrineExtensions

Doctrine2 behavioral extensions, Translatable, Sluggable, Tree-NestedSet, Timestampable, Loggable, Sortable
MIT License
4.01k stars 1.26k forks source link

[Loggable] restore data without flush #2745

Closed Fipschen closed 5 months ago

Fipschen commented 5 months ago

Environment

Ubuntu 22.04, PHP 8.3.1, MySQL 8.0.35 Symfony 7.0

Package

show

``` name : gedmo/doctrine-extensions descrip. : Doctrine behavioral extensions keywords : Blameable, behaviors, doctrine, extensions, gedmo, loggable, nestedset, odm, orm, sluggable, sortable, timestampable, translatable, tree, uploadable versions : * v3.14.0 latest : v3.14.0 type : library license : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText homepage : http://gediminasm.org/ source : [git] https://github.com/doctrine-extensions/DoctrineExtensions.git 3b5b5cba476b4ae32a55ef69ef2e59d64d5893cf dist : [zip] https://api.github.com/repos/doctrine-extensions/DoctrineExtensions/zipball/3b5b5cba476b4ae32a55ef69ef2e59d64d5893cf 3b5b5cba476b4ae32a55ef69ef2e59d64d5893cf path : /var/www/html/vendor/gedmo/doctrine-extensions names : gedmo/doctrine-extensions support email : gediminas.morkevicius@gmail.com issues : https://github.com/doctrine-extensions/DoctrineExtensions/issues source : https://github.com/doctrine-extensions/DoctrineExtensions/tree/v3.14.0 wiki : https://github.com/Atlantic18/DoctrineExtensions/tree/main/doc autoload psr-4 Gedmo\ => src/ requires behat/transliterator ^1.2 doctrine/annotations ^1.13 || ^2.0 doctrine/collections ^1.2 || ^2.0 doctrine/common ^2.13 || ^3.0 doctrine/event-manager ^1.2 || ^2.0 doctrine/persistence ^2.2 || ^3.0 php ^7.4 || ^8.0 psr/cache ^1 || ^2 || ^3 symfony/cache ^5.4 || ^6.0 || ^7.0 symfony/deprecation-contracts ^2.1 || ^3.0 requires (dev) doctrine/cache ^1.11 || ^2.0 doctrine/dbal ^3.2 doctrine/doctrine-bundle ^2.3 doctrine/mongodb-odm ^2.3 doctrine/orm ^2.14.0 friendsofphp/php-cs-fixer ^3.14.0 nesbot/carbon ^2.71 || 3.x-dev as 3.0 phpstan/phpstan ^1.10.2 phpstan/phpstan-doctrine ^1.0 phpstan/phpstan-phpunit ^1.0 phpunit/phpunit ^9.6 rector/rector ^0.18 symfony/console ^5.4 || ^6.0 || ^7.0 symfony/phpunit-bridge ^6.0 || ^7.0 symfony/yaml ^5.4 || ^6.0 || ^7.0 suggests doctrine/mongodb-odm to use the extensions with the MongoDB ODM doctrine/orm to use the extensions with the ORM conflicts doctrine/dbal <3.2 doctrine/mongodb-odm <2.3 doctrine/orm <2.14.0 || 2.16.0 || 2.16.1 sebastian/comparator <2.0 ```

Doctrine packages

show

``` Direct dependencies required in composer.json: doctrine/annotations 2.0.1 2.0.1 Docblock Annotations Parser doctrine/doctrine-bundle 2.11.1 2.11.1 Symfony DoctrineBundle doctrine/doctrine-migrations-bundle 3.3.0 3.3.0 Symfony DoctrineMigrationsBundle doctrine/orm 2.17.2 2.17.2 Object-Relational-Mapper for PHP Transitive dependencies not required in composer.json: doctrine/cache 2.2.0 2.2.0 PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as re... doctrine/collections 2.1.4 2.1.4 PHP Doctrine Collections library that adds additional functionality on top of PHP arrays. doctrine/common 3.4.3 3.4.3 PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects... doctrine/dbal 3.7.2 3.7.2 Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and mana... doctrine/deprecations 1.1.2 1.1.2 A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprec... doctrine/event-manager 2.0.0 2.0.0 The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine ... doctrine/inflector 2.0.8 2.0.8 PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowerca... doctrine/instantiator 2.0.0 2.0.0 A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer 2.1.0 3.0.0 PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers. doctrine/migrations 3.7.2 3.7.2 PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBA... doctrine/persistence 3.2.0 3.2.0 The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine... doctrine/sql-formatter 1.1.3 1.1.3 a PHP SQL highlighting library ```

PHP version

PHP 8.3.1 (cli) (built: Dec 21 2023 20:12:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.1, Copyright (c), by Zend Technologies

Subject

When I use $repo->revert($user, $version); the Entity is restored in the database WITHOUT doctrine->persist/flush. It's not possible to use revert to load the old version into the entity without restore it in the database. If I use beginTransaction() and rollback() it's also restored.

Code for use Loggable:

$this->eM->beginTransaction();
$repo = $this->eM->getRepository(LogEntry::class);
$user = $this->userRepos->findOneBy(['id' => 7]);
echo $user->getVorname();
$logs = $repo->getLogEntries($user);
$repo->revert($user, 10);
echo $user->getVorname();
dump($logs);
$this->eM->rollback();

Short version of my entity:

namespace App\Entity\User;

[...]
use Gedmo\Mapping\Annotation as Gedmo;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[Gedmo\Loggable]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 40)]
    #[Gedmo\Versioned]
    #[Assert\NotBlank(message: "Gib deinen Vornamen an")]
    private ?string $vorname = null;

    #[ORM\Column(length: 40)]
    #[Gedmo\Versioned]
    #[Assert\NotBlank(message: "Gib deinen Nachnamen an")]
    private ?string $nachname = null;

    [...]
    Methods
    [...]

    #[ORM\PrePersist]
    #[ORM\PreUpdate]
    public function setLastChange()
    {
        $this->last_change = new \DateTime();

        return $this;
    }
}

This bug is reproducible with others Entities. I've also removed the ORM Lifecyclecallbacks, but the behavior is the same.

How can I stop the automatic restore?

Thank you very much

Fipschen commented 5 months ago

I've found my problem: A function from me in the FinishRequestEvent uses flush and had flushed everything. I've solved it with $em->flush($once_entity);