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 6852253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:20:56+00:00 2026-05-27T01:20:56+00:00

I’m working on a Symfony2 project at the moment. For the most part it’s

  • 0

I’m working on a Symfony2 project at the moment. For the most part it’s totally standard; I’m using the ORM layer to interface with the database via my Entities. No problems there.

However, I do need to make infrequent queries to a small handful of tables in an existing schema elsewhere in the system, which contains what I would call ‘reference’ information: things like currency conversion ratios and such. I have SELECT only access to this schema.

I set up another connection and I have been dropping to the DBAL layer to make the queries on this schema, which has been working pretty well so far.

My issue is that, although infrequent, I think I’ll need to repeat some of my DBAL queries in more than one place in my app; I would like to refactor these queries into some sort of repository, where they are more easily used/tested/etc. I thought about creating Entities for the tables, but I feel this is overkill in this case. Am I correct in thinking that you need Entities to create a repository?

Instead I am wondering if there is a ‘Symfony way’ to do this? Something nice and elegant 🙂

Thanks!
Darragh

  • 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-05-27T01:20:56+00:00Added an answer on May 27, 2026 at 1:20 am

    Update

    2013-10-03

    Forgive me for editing a two year old answer… However a couple of people have questioned the existing approach, and while it works (and worked appropriately well for my particular use case), defining services is of course the Symfony way.

    Nobody provided an example so, for reference/completeness, I will update my answer. I have to admit I wasn’t really au fait with defining custom services when I originally posted this answer, but we live and learn.

    The original answer is preserved below.

    1. Create an additional DBAL connection

    • Create connection foo in app/config/config.yml.
    • Argument wrapper_class is not required in this case (see original answer).
    doctrine:
        dbal:
            connections:
                default:
                    driver:   %database_driver%
                    host:     %database_host%
                    dbname:   %database_name%
                    user:     %database_user%
                foo:
                    driver:   %foo_driver%
                    host:     %foo_host%
                    dbname:   %foo_name%
                    user:     %foo_user%
    

    2. Configure service

    • Assuming YAML format.
    • Add configuration to src/Acme/TestBundle/Resources/config/services.yml.
    • Note, we are injecting the above defined DBAL foo_connection into the service.
    services:
        foo_query_service:
            class: Acme\TestBundle\Services\FooQueryService
            arguments:
                - @doctrine.dbal.foo_connection
    

    3. Create class for the configured service

    • Create the following class at src/Acme/TestBundle/Services/FooQueryService.php:
    <?php
    
    namespace Acme\TestBundle\Services;
    
    use DateTime;
    use Doctrine\DBAL\Connection;
    
    class FooQueryService
    {
        private $connection;
    
        public function __construct(Connection $connection)
        {
            $this->connection = $connection;
        }
    
        public function findBarByDate(DateTime $date)
        {
            $stmt = $this->connection->prepare('SELECT * FROM bar WHERE date = :date');
            $stmt->bindValue('date', $date, 'datetime');
            $stmt->execute();
    
            return $stmt->fetch();
        }
    }
    

    4. Finally, use your queries wherever you need them!

    For example, in a controller…

    /**
     * @Route("/", name="home")
     * @Template()
     */
    public function indexAction()
    {
        $date = new \DateTime();
    
        $result = $this->get('foo_query_service')
            ->findBarByDate($date);
    
        return array();
    }
    

    Done 🙂 Thanks to Acayra and koskoz for their feedback.


    Okay, I think I found a solution that works for me in this instance.

    I actually had another look at creating entities/managers – actually the Symfony2 documentation around mapping specific entities to multiple managers seems to be lacking. It still seems like an overkill approach in this instance (and the ‘reference’ schemas are pretty messy).

    Fortunately, it’s possible to specify a wrapper class for a DBAL connection and abstract queries into specific methods there.

    1. Create an additional DBAL connection with a wrapper class in config.yml:
    doctrine:
        orm:
            connections:
                default:
                    driver:   %driver%
                    host:     %host%
                    dbname:   %name%
                    user:     %user%
                foo:
                    wrapper_class: 'Acme\TestBundle\Doctrine\DBAL\FooConnection'
                    driver:   %foo_driver%
                    host:     %foo_host%
                    dbname:   %foo_name%
                    user:     %foo_user%
    
    1. Create the wrapper class at the path specified:
    <?php
    
    namespace Acme\TestBundle\Doctrine\DBAL\FooConnection;
    use Doctrine\DBAL\Connection;
    
    class FooConnection extends Connection
    {
        // custom query...
        public function findBarByDate(\DateTime $date)
        {
            $stmt = $this->prepare('SELECT * FROM bar WHERE date = :date');
            $stmt->bindValue('date', $date, 'datetime');
            $stmt->execute();  
    
            return $stmt->fetch();
        }
    }
    

    Note that the wrapper class must extend \Doctrine\DBAL\Connection.

    1. Use your queries wherever you need them:
    $date   = new \DateTime();
    $conn   = $this->getDoctrine()->getConnection('foo');
    $result = $conn->findBarByDate($date);
    

    Hope this helps!

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in

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.