0

I'm trying to upgrade an app from Symfony 3 to 4 (currently on 3.4), and I'm looking at a deprecation warning regarding Sonata 3 config that I'm not seeing how to fix:

Relying on service auto-registration for type "AppBundle\Entity\Box" is deprecated since Symfony 3.4 and won't be supported in 4.0.
Create a service named "AppBundle\Entity\Box" instead.
-severity: E_USER_DEPRECATED
trace: {
  /Users/me/Sites/app-php-7/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php:321 {
    › if (!$this->strictMode) {
    ›     return $this->createAutowiredDefinition($type);
    › }
  }
}

Well, documentation says to not wire Entity classes as services, so I'm scratching my head at the pre-existing Sonata config (in app/config/services/admins.yml):

admin.box:
    class: AppBundle\Admin\BoxAdmin
    arguments: [ ~, AppBundle\Entity\Box, ~ ]
    tags:
        - { name: sonata.admin, manager_type: orm, label: Box }

Note that 'Box' here just refers to an example real-world object, like a cardboard box whose data we're saving to a database table

Anyway, I've tried different things, like changing AppBundle\Entity\Box to '@AppBundle\Entity\Box' (which of course makes Symfony look for its service which it has no definition for, and crashes the app), or 'AppBundle\Entity\Box' which doesn't doesn't get rid of the deprecation warning.

I've tried adding container.autowiring.strict_mode: true to parameters.yml, but to no avail.

I'm completely lost on what to do here. Maybe I've missed something in docs, but I've scoured Symfony 2.8, 3.4, 4.0 docs and Sonata 3.x docs.

SteveExdia
  • 321
  • 1
  • 3
  • 11
  • Be aware that 4.4 is the last version supported by Sonata so if you plan on eventually moving to Symfony 5/6+ then it's time to start thinking about using something different. All I can suggest in this particular case is to create a new 4.4 project and install the Sonata bundle. Maybe that will give you an idea of what your config eventually needs to look like. – Cerad Jul 21 '22 at 12:27
  • Yes I'm aware of the upgrade restrictions (I see the composer abandoned warnings for Sonata and others every time I run it), however, Symfony devs recommend upgrading Symfony 3.4 to 4 to 4.4 to 5 to 6, and to fix deprecation warnings before upgrade... so... that's where I'm at. Fixing Sonata deprecation warnings. I've been told by multiple devs (including my more-tenured coworker) that starting a new repo at 4.4 is a massive waste of time. So, I don't know who to believe. Sonata's docs are sorely lacking on config explanation, and ours is weird. Maybe I should show how it is in my question... – SteveExdia Jul 21 '22 at 21:29
  • Given a choice between an experienced coworker and some random internet guy then I'd pick the coworker as well. Even if the random guy has been answering Symfony questions since the original 2.0 release. However, I would still wonder why I am working with a bundle that I know will need to be replaced. An unfamiliar bundle for which I have to resort to trying random configuration changes in an attempt to get something working. I myself would invest in what should be a minimal amount of time creating a simple test project just to see how things fit together. Good luck. – Cerad Jul 22 '22 at 12:55
  • And I think I was wrong about the need to replace the bundle. Just out of curiosity I tried installing it on a new Symfony 6.1 project and it seemed to install cleanly. Looking at the [example admin service configuration](https://symfony.com/bundles/SonataAdminBundle/current/getting_started/creating_an_admin.html#step-2-register-the-admin-class) in the docs, you might try just commenting out your arguments section completely. Maybe it is interfering with Symfony's autowiring. – Cerad Jul 22 '22 at 13:15
  • @Cerad It's not just my coworker that recommended against just migrating to a higher version... you're the first dev of a handful that's said that, even though I still consider it feasible (I'm just not very experienced at Symfony). ... I don't need to wonder why I'm working with an abandoned bundle... I have to work with it _until_ it gets replaced. I'm just trying to fix deprecation warnings in preparation for the Symfony 4 upgrade. I think you're forgetting things I've said as you write. I'm sorry this is so complicated. Symfony 3 is overly-complicated. I can't wait to get past it. – SteveExdia Jul 26 '22 at 20:05
  • @Cerad In the meantime, we're not planning on replacing Sonata until after the Symfony 4 or 5 upgrade, because it's so integrated into the app. I just want to get to Symfony 4 first because it's so much easier to work with than 3. Fixing the deprecation warning still looks easier than replacing the Sonata integration, with... I don't even know what. So, if you have advice on _the original question I had_, figuring out how to clean up the config, the specific formatting example i mentioned, please, I'd love to hear about that for now... I'm finding no documentation on it... just trial-and-error – SteveExdia Jul 26 '22 at 20:08
  • You might try the accepted answer [here](https://stackoverflow.com/questions/47907721/relying-on-service-auto-registration-error-on-orm-entity). It's really a false positive message which can be ignored with `container.autowiring.strict_mode: true`. Or excluding your AppBundle\Admin classes from autowire in services.yml. – Cerad Jul 26 '22 at 21:06
  • I set `container.autowiring.strict_mode: true` in parameters.yml and haven't seen the warning again. I haven't seen excluding AppBundle\Admin classes from autowire suggested previously, and I'm not sure I should do that yet. – SteveExdia Jul 28 '22 at 14:18
  • If you don't exclude it then autowire will try to wire it up and will get stuck because it won't know what arguments to inject into the constructor. Try it and see. – Cerad Jul 28 '22 at 15:26

0 Answers0