Skip to content
Advertisement

Symfony 4 – Autowiring not working

I have a form that write values to an specific Entity, and I am trying to edit the information through a function in the controller, pretty simple.

BancoController.php

JavaScript

The problem is, when I access /banco/{id}, I get the error:

JavaScript

My service.yaml is all default, so I guess it should work automatically. The entities doesn’t show in bin/console debug:container.

If I declare manually the Entity in services.yaml like this

JavaScript

it works, but now when I access /banco/{id} the form comes empty without the information that exists in the database, and if I type something and submit it, nothing changes in the database.

If I go to the debug toolbar and check the query, it appears that it is querying the ID of the user logged in, and not the ID of the Entity ‘Banco’.

Btw, this Entity Table has a FK user_id. Maybe that’s where lies the problem? I am lost so I need a little help. I am new to PHP/Symfony. Thank you

Advertisement

Answer

The Symfony will autowire the Entity but it will be an empty one. Better way is to autowire the Repository, get the ID from the Request and fetch the Banco from the Repository by it’s ID.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement