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

  • Home
  • SEARCH
  • 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 8207825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:07:32+00:00 2026-06-07T09:07:32+00:00

Would be possible to have a custom repository not associated with an entity in

  • 0

Would be possible to have a custom repository not associated with an entity in Symfony 2 and Doctrine 2? I would like to put in it some native SQL that doesn’t fit well in other repositories (it may refer to abstract or entity hierarchy).

How controller code $this->getDoctrine()->getRepositoty(/* ??? */) should be replaced?

  • 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-07T09:07:34+00:00Added an answer on June 7, 2026 at 9:07 am

    It’s possible to have as many repositories as you wish. However, only a single repository can be linked with the entity manager.

    You need to define a few services to add a custom repository.

    <!-- My custom repository -->
    <service id="acme.repository.my_entity" class="Acme\FQCN\MyEntityRepository" >
        <argument type="service" id="doctrine.orm.entity_manager" />
        <argument type="service" id="acme.metadata.my_entity" />
    </service>
    
    <!-- MyEntity metadata -->
    <service id="acme.metadata.my_entity" class="Doctrine\ORM\Mapping\ClassMetaData">
        <argument>Acme\FQCN\MyEntity</argument>
    </service>
    

    The repository class would have to inherit from EntityRepository.

    namespace Acme\FQCN;
    
    use Doctrine\ORM\EntityRepository;
    
    class MyEntityRepository extends EntityRepository
    {
        /** 
         * If you want to inject any custom dependencies, you'd have either have to
         * add them to the construct or create setters. I'd suggest using setters
         * in which case you wouldn't need to use the constructor in this class.
         *
         * public function __construct($em, Doctrine\ORM\Mapping\ClassMetadata $class, $custom_dependency)
         * {
         *     parent::__construct($em, $class);
         * }
         *
         */
    }
    

    Unfortunately you’ll not be able to retrieve it via the doctrine service. Instead, retrieve it straight from the container:

    $this->get('acme.repository.my_entity');
    

    EDIT

    If you’re creating a repository that shouldn’t be linked to any entities, simply create a service and inject the necessary dependencies.

    <!-- Repository for misc queries -->
    <service id="acme.repository.misc" class="Acme\FQCN\MiscRepsitory">
        <argument type="service" id="database_connection" />
    </service>
    

    Since you’re not using any of the Doctrine’s ORM features in a custom repository, there’s no need to extend EntityManager.

    namespace Acme\FQCN;
    
    use \Doctrine\DBAL\Connection;
    
    class MiscRepository
    {
        protected $conn;
    
        public function __construct(Connection $conn)
        {
            $this->conn = $conn;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have custom datatype in powershell? I would like to have
I do not plan on using Weld, but still would like to have custom
I would like to know if it is possible to have subquery in a
As python does not have concept of constants, would it be possible to raise
Is it possible to set branch permissions with Gitorious? I would like to have
I have a custom piece of Javascript which I would like to run on
I have an irregular shaped custom button and I would like for it to
I have a custom config file format and I would like Emacs to recognize
I have custom error pages setup in my ASP.Net 4.0 application. I would like
I have several custom reports and I would like to be able to add

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.