doctrine-extensions / DoctrineExtensions

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

Doctrine subscriber is deprecated #2704

Closed oleg-andreyev closed 5 months ago

oleg-andreyev commented 1 year ago
Registering \"Gedmo\\Timestampable\\TimestampableListener\" as a Doctrine subscriber is deprecated. Register it as a listener instead, using e.g. the #[AsDoctrineListener] attribute.

Environment

Package

show

``` Info from https://repo.packagist.org: #StandWithUkraine 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.13.0 latest : v3.13.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 291d0c527d2dc9ee07b888c9a4e2a179893f08ab dist : [zip] https://api.github.com/repos/doctrine-extensions/DoctrineExtensions/zipball/291d0c527d2dc9ee07b888c9a4e2a179893f08ab 291d0c527d2dc9ee07b888c9a4e2a179893f08ab path : /app/vendor/gedmo/doctrine-extensions names : gedmo/doctrine-extensions ```

Doctrine packages

show

``` Color legend: - patch or minor release available - update recommended - major release available - update possible - up to date version Direct dependencies required in composer.json: doctrine/annotations 2.0.1 2.0.1 Docblock Annotations Parser doctrine/doctrine-bundle 2.10.2 2.10.2 Symfony DoctrineBundle doctrine/doctrine-migrations-bundle 3.2.4 3.2.4 Symfony DoctrineMigrationsBundle doctrine/orm 2.16.2 2.16.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 redis, memcache, apc, mongodb and others. doctrine/collections 1.8.0 2.1.3 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 depend on such as better reflection support, proxies and ... doctrine/dbal 3.6.7 3.7.0 Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management. doctrine/deprecations v1.1.1 1.1.2 A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages. 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 projects. doctrine/inflector 2.0.8 2.0.8 PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words. 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.6.0 3.6.0 PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deployin... doctrine/persistence 3.2.0 3.2.0 The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share. doctrine/sql-formatter 1.1.3 1.1.3 a PHP SQL highlighting library ```

PHP version

PHP 8.1.23 (cli) (built: Sep  2 2023 08:00:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.23, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.23, Copyright (c), by Zend Technologies
    with Xdebug v3.2.2, Copyright (c) 2002-2023, by Derick Rethans

Subject

Minimal repository with the bug

Steps to reproduce

Expected results

Actual results

ozahorulia commented 1 year ago

Same here, but in different context

User Deprecated: Method Doctrine\ORM\Event\OnFlushEventArgs::getEntityManager() is deprecated and will be removed in Doctrine ORM 3.0. Use getObjectManager() instead. (OnFlushEventArgs.php:34 called by ORM.php:98, https://github.com/doctrine/orm/issues/9875, package doctrine/orm)
pculka commented 1 year ago

Any possible updates on this?

LaurentSanson commented 1 year ago

Waiting for any update too

stof commented 1 year ago

This is not deprecated in Doctrine itself. However, Symfony has deprecated registering Doctrine subscribers because they cannot be lazy-loaded (contrary to Symfony subscribers, the Doctrine ones require instantiating the object to get the list of subscribed events).

In https://packagist.org/packages/stof/doctrine-extensions-bundle, I already updated the way listeners are registered to be compatible with this lazy-loading (include tests that will ensure that the explicit registration is in sync with the library subscriber). If you manage the listener yourselves instead of using the bundle, you need to manage the registration yourselves.

LaurentSanson commented 1 year ago

Just found my answer in the documentation : https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/symfony.md#doctrine-extension-listener-services

Needed to update the config in services.yaml

mcanepa commented 11 months ago

@LaurentSanson I get

The service "gedmo.listener.tree" has a dependency on a non-existent service "annotation_reader"

how did you solve it?

LaurentSanson commented 11 months ago

@mcanepa ,

You could just remove the call to annotation reader that you've set in your config file, or better option, use the @stof 's bundle properly and in hisstof_doctrine_extensions_yamlset something like this :

# Read the documentation: https://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html
# See the official DoctrineExtensions documentation for more details: https://github.com/doctrine-extensions/DoctrineExtensions/tree/main/doc
stof_doctrine_extensions:
    default_locale: fr_FR
    orm:
        default:
            timestampable: true
            sluggable: true

It should works just fine

mcanepa commented 11 months ago

@LaurentSanson I did both:

In config/services.yaml I removed

        calls:
            - [ setAnnotationReader, [ "@annotation_reader" ] ]

and instead of using

    gedmo.listener.timestampable:
        class: Gedmo\Timestampable\TimestampableListener
        tags:
            - { name: doctrine.event_listener, event: 'prePersist' }
            - { name: doctrine.event_listener, event: 'onFlush' }
            - { name: doctrine.event_listener, event: 'loadClassMetadata' }

I used config/stof_doctrine_extensions.yaml

stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            timestampable: true

now TimestampableEntity trait works but only for POST operation, it fails on PUT. I get

An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null"

LaurentSanson commented 11 months ago

I think you can get rid of the calling of timestampable in your services.yaml as it is enabled in stof_doctrine_extensions.yaml

github-actions[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

oleg-andreyev commented 5 months ago

https://github.com/doctrine-extensions/DoctrineExtensions/issues/2704#issuecomment-1777435330