DoctrineExtensions icon indicating copy to clipboard operation
DoctrineExtensions copied to clipboard

Doctrine subscriber is deprecated

Open oleg-andreyev opened this issue 2 years ago • 9 comments

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

oleg-andreyev avatar Sep 29 '23 08:09 oleg-andreyev

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)

ozahorulia avatar Oct 06 '23 21:10 ozahorulia

Any possible updates on this?

pculka avatar Oct 15 '23 17:10 pculka

Waiting for any update too

LaurentSanson avatar Oct 19 '23 13:10 LaurentSanson

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.

stof avatar Oct 24 '23 15:10 stof

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

LaurentSanson avatar Oct 25 '23 08:10 LaurentSanson

@LaurentSanson I get

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

how did you solve it?

mcanepa avatar Dec 11 '23 20:12 mcanepa

@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

LaurentSanson avatar Dec 11 '23 20:12 LaurentSanson

@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"

mcanepa avatar Dec 13 '23 06:12 mcanepa

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

LaurentSanson avatar Dec 14 '23 06:12 LaurentSanson

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.

github-actions[bot] avatar Jun 11 '24 09:06 github-actions[bot]

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

oleg-andreyev avatar Jun 11 '24 13:06 oleg-andreyev