2

I don't want to use Symfony2 doctrine. Instead want to write own data classes to handle MySQL queries. So is there any way that directly sql queries can be executed. Most article in google talks about Doctrine or MySQL+Doctrine.

1

2 Answers 2

3

If you don't want to use Doctrine ORM or even Doctrine DBAL, absolutley nothing stopes you from using PDO/MySQLi directly.

  1. Define PDO instance as DIC service:

    <service id="pdo" class="PDO">
        <argument>dns</argument>
        <argument>user</argument>
        <argument>password</argument>
        <call method="setAttribute">
            <argument>2</argument> <!-- use exception for error handling -->
        </call>
    </service>
    
  2. Pass PDO instance for each service that requires database connection:

    <service id="my.custom.service" class="My\Custom\Service">
         <argument type="service" id="pdo" />
    </serivce>
    
    ---
    
    namespace My\Custom;
    
    class Service {
        public function __construct(PDO $pdo) { }
    }
    
Sign up to request clarification or add additional context in comments.

Comments

0

There's a cookbook about using Doctrine's DBAL Layer.

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.