0

I am trying to separate data access from my controllers in a Symfony 3 application. Symfony allows Doctrine mediated DB queries to be made via a repository class that extends the EntityRepository class, but I'm having trouble finding a way to persist objects to the DB outside of a class that inherits from the controller base class. Simply put: How can I persist an Entity object to a DB via Doctrine outside of a controller in Symfony?

Thanks for your help.

2
  • do you mean to persist the obj from within the controller method? if so - symfony.com/doc/current/book/… Commented Jun 28, 2016 at 22:23
  • Please add some code to show where do you want to persist your data. Commented Jun 29, 2016 at 6:30

1 Answer 1

1

You have two options ,

Say that the class you want to persist objects with a class named DBManager

  1. Define DBManager as a service and inject doctrine entity manager into this class. This thread may help you
  2. Make DBManger extends ContainerAware, like that you will already have entity manager along with other services like mailer, logger, etc. Take a look at this answer for more details on how to do it
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.