{"id":6366,"date":"2014-04-17T00:57:50","date_gmt":"2014-04-17T00:57:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/17\/how-do-you-access-doctrine-dbal-in-a-symfony2-service-class-collection-of-common-programming-errors\/"},"modified":"2014-04-17T00:57:50","modified_gmt":"2014-04-17T00:57:50","slug":"how-do-you-access-doctrine-dbal-in-a-symfony2-service-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/17\/how-do-you-access-doctrine-dbal-in-a-symfony2-service-class-collection-of-common-programming-errors\/","title":{"rendered":"How do you access Doctrine DBAL in a Symfony2 service class?-Collection of common programming errors"},"content":{"rendered":"<p>First off you should add a constructor to your class and pass in the @doctrine.dbal.connection service<\/p>\n<pre><code>namespace Acme\\TestBundle\\Toolbox;\nuse Doctrine\\DBAL\\Connection;\n\nclass StringToolbox\n{\n    \/**\n    *\n    * @var Connection\n    *\/\n    private $connection;\n\n    public function __construct(Connection $dbalConnection)  {\n        $this-&gt;connection = $dbalConnection;    \n    }\n\n    public function lookupSomething($foo)\n    {\n\n    $sql = \"SELECT bar FROM bar_list WHERE foo = :foo\";\n    $stmt = $this-&gt;connection-&gt;prepare($sql);\n    $stmt-&gt;bindValue(\"foo\", $foo);\n    $stmt-&gt;execute();\n\n\n    return $bar;\n    }\n\n\n}\n<\/code><\/pre>\n<p>Your service configuration should now look like this:<\/p>\n<pre><code>services:\n toolbox:\n   class:        Acme\\TestBundle\\Toolbox\\StringToolbox\n    arguments:   [@doctrine.dbal.connection]\n<\/code><\/pre>\n<p>What you are saying with this configuration is &#8220;make me a service named toolbox that will receive the doctrine.dbal.connection service as the first constructor argument&#8221;<\/p>\n<p>There are other injection methods besides Constructor injection and you should read the http:\/\/symfony.com\/doc\/current\/book\/service_container.html documentation to get a grasp of all possibilities (Setter injection, Factory injection, etc) and to better understand how Dependency Injection works<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First off you should add a constructor to your class and pass in the @doctrine.dbal.connection service namespace Acme\\TestBundle\\Toolbox; use Doctrine\\DBAL\\Connection; class StringToolbox { \/** * * @var Connection *\/ private $connection; public function __construct(Connection $dbalConnection) { $this-&gt;connection = $dbalConnection; } public function lookupSomething($foo) { $sql = &#8220;SELECT bar FROM bar_list WHERE foo = :foo&#8221;; $stmt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6366","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6366"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6366\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}