Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To persist entities, you need to first inject the entity manager into your class.</p> <pre><code>class ChatTopic implements TopicHandlerInterface { protected $em; public function __construct($em) { $this-&gt;em = $em; } } </code></pre> <p>You'll need to inject the dependency in services.xml</p> <pre><code>&lt;services&gt; &lt;service id="jdare_clank.chat_topic_handler" class="JDare\ClankChatBundle\Topic\ChatTopic"&gt; &lt;argument&gt;"@doctrine.orm.default_entity_manager"&lt;/argument&gt; &lt;/service&gt; </code></pre> <p>And get your class from the service container in a controller or some other ContainerAwareInterface:</p> <pre><code>$chatTopic = $this-&gt;getContainer()-&gt;get('jdare_clank.chat_topic_handler'); </code></pre> <p>Getting the user is trickier, since you won't have access to the security context in that class, since it is not container aware. There are a few ways you could do it. In our case, we actually implemented a secure websocket (wss) protocol and created a login protocol within, so we could store a user id with each connection. But one quick and dirty way would be to simply add the user id to your session in another controller.</p> <pre><code>$userId = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser()-&gt;getId(); $session = $this-&gt;get('session'); $session-&gt;set('user', (str) $userId); </code></pre> <p>Then you can get the user from the session inside your class.</p> <pre><code>public function onSubscribe(Conn $conn, $topic) { $userId = $conn-&gt;Session-&gt;get('user'); $user = $this-&gt;em-&gt;getRepository('AcmeUserBundle:User')-&gt;find((int) $userId); ... </code></pre> <p>Hope this is helpful. Let me know if any of this loses you, and I'll try to help. Dependency injection is a little hard to wrap your head around, but it's a very powerfool tool in your kit!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload