# DIC in Subscribern benutzen

**URL:** <https://forum.shopware.com/t/dic-in-subscribern-benutzen/45503>\
**Category:** Programmierung\
**Created:** [30. April 2017 um 16:58 UTC](https://forum.shopware.com/t/dic-in-subscribern-benutzen/45503 "2017-04-30T16:58:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Misengo](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/misengo/32/21446_2.png) [@Misengo](https://forum.shopware.com/u/Misengo)\
**Post date:** [30. April 2017 um 16:58 UTC](https://forum.shopware.com/t/dic-in-subscribern-benutzen/45503/1 "2017-04-30T16:58:02Z")

</div>

Ich bastel gerade an einem Plugin und wundere mich warum ich nicht Standardmäßg auf die DI Container zugreifen kann. Ich möchte gerne einige DB Queries einsetzen ($this-\>container-\>get(‚dbal\_connection‘)) - mit&nbsp; **&nbsp;** übergebe ich ja eigentlich in meiner services.xml eine Instanz des DI Containers.

Jemand ne Idee?

---

<div class="post-metadata">

**Author:** ![Thomas](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/thomas/32/8167_2.png) [@Thomas](https://forum.shopware.com/u/Thomas)\
**Post date:** [1. Mai 2017 um 12:01 UTC](https://forum.shopware.com/t/dic-in-subscribern-benutzen/45503/2 "2017-05-01T12:01:15Z")

</div>

Warum möchtest Du auf den ganzen DI-Container zugreifen wenn Du nur die Dbal\_Connection benötigst?&nbsp;

Folgendes snippet sollte funktionieren:  
services.xml

EventSubscriber.php  
&nbsp;

```
connection = $connection;
    }

    public static function getSubscribedEvents()
    {
        return ['Enlight_Controller_Action_PostDispatchSecure_Frontend' => 'onPostDispatchSecure'];
    }

    public function onPostDispatchSecure(\Enlight_Event_EventArgs $args)
    {
        $articles = $this->connection->fetchAll('SELECT * FROM s_articles LIMIT 10');
    }
}

```

&nbsp;

Ist nicht getestet, sollte aber passen 🙂  
&nbsp;

---

<div class="post-metadata">

**Author:** ![Misengo](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/misengo/32/21446_2.png) [@Misengo](https://forum.shopware.com/u/Misengo)\
**Post date:** [16. Mai 2017 um 09:38 UTC](https://forum.shopware.com/t/dic-in-subscribern-benutzen/45503/3 "2017-05-16T09:38:08Z")

</div>

> [@Thomas schrieb:](https://forum.shopware.com/profile/16897/Thomas "Thomas")
> 
> Warum möchtest Du auf den ganzen DI-Container zugreifen wenn Du nur die Dbal\_Connection benötigst?&nbsp;
> 
> Folgendes snippet sollte funktionieren:  
> services.xml
> 
> [http://symfony.com/schema/dic/services](http://symfony.com/schema/dic/services)";  
> xmlns:xsi=„[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)“;  
> xsi:schemaLocation=„[http://symfony.com/schema/dic/services](http://symfony.com/schema/dic/services) [http://symfony.com/schema/dic/services/services-1.0.xsd](http://symfony.com/schema/dic/services/services-1.0.xsd)“\>;
> 
> EventSubscriber.php  
> &nbsp;
> 
> connection = $connection;  
> }
> 
> public static function getSubscribedEvents()  
> {  
> return [‚Enlight\_Controller\_Action\_PostDispatchSecure\_Frontend‘ =\> ‚onPostDispatchSecure‘];  
> }
> 
> public function onPostDispatchSecure(\Enlight\_Event\_EventArgs $args)  
> {  
> $articles = $this-\>connection-\>fetchAll(‚SELECT \* FROM s\_articles LIMIT 10‘);  
> }  
> }
> 
> &nbsp;
> 
> Ist nicht getestet, sollte aber passen ![:)](https://dub1.discourse-cdn.com/flex013/resources/emoji/smile.png ":)")  
> &nbsp;

Danke dir!&nbsp;
