Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8388431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:28:18+00:00 2026-06-09T18:28:18+00:00

In a Symfony2 project, when you use a Controller , you can access Doctrine

  • 0

In a Symfony2 project, when you use a Controller, you can access Doctrine by calling getDoctrine() on this, i.e.:

$this->getDoctrine();

In this way, I can access the repository of such a Doctrine Entity.

Suppose to have a generic PHP class in a Symfony2 project. How can I retrieve Doctrine ?
I suppose that there is such a service to get it, but I don’t know which one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:28:19+00:00Added an answer on June 9, 2026 at 6:28 pm

    You can register this class as a service and inject whatever other services into it. Suppose you have GenericClass.php as follows:

    class GenericClass
    {
        public function __construct()
        {
            // some cool stuff
        }
    }
    

    You can register it as service (in your bundle’s Resources/config/service.yml|xml usually) and inject Doctrine’s entity manager into it:

    services:
        my_mailer:
            class: Path/To/GenericClass
            arguments: [doctrine.orm.entity_manager]
    

    And it’ll try to inject entity manager to (by default) constructor of GenericClass. So you just have to add argument for it:

    public function __construct($entityManager)
    {
         // do something awesome with entity manager
    }
    

    If you are not sure what services are available in your application’s DI container, you can find out by using command line tool: php app/console container:debug and it’ll list all available services along with their aliases and classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Capifony to deploy my Symfony2 project to the production server. As a
I have a Symfony2 project with a MySQL db: #app/config/config.yml doctrine: dbal: driver: %database_driver%
When I update my Symfony2 project I always have to do this: root@xxx:/var/www/project# php
How can I create a new project that only uses the Symfony2 Console component?
I am working on the project that is based on symfony (doctrine) with use
For my Symfony2 project, I'd like to stick with the Fat Model Skinny Controller
Actually, this is two questions: What is the correct way to use the SCSS
I have added the translator service for my Symfony2 project. I use it both
I use Doctrine2 as ORM in a Symfony2 project. I have article and tag
Is it possible to use Twig in Silex from Symfony2 project instead from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.