title: Symfony2 access user and doctrine in a service
body: <p>I'm running the equivalent of this code in lots and lots of controller actions, basically it grabs the user's username, and if that username is attached to a blog entity it will allow the user to see the blog entity(s):</p>
  
  <pre><code>    $em = $this-&gt;getDoctrine()-&gt;getManager();
      $user = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
      $entities = $em-&gt;getRepository('MySiteBundle:Blog')-&gt;findBy(array('user' =&gt; $user));
  
      return $this-&gt;render('MySiteBundle:Blog:index.html.twig', array(
          'entities' =&gt; $entities,
  </code></pre>
  
  <p>I want to move it into a service so I can cut down on code repetition. I want to avoid doing as much logic in my controllers as possible.  </p>
  
  <p>That being said, I'm not sure how I can access the user session and doctrine in a service.</p>
  
  <p>Here's my services.yml:</p>
  
  <pre><code>mysite.user.blog:
      class: MySite\SiteBundle\Services\BlogUser
  </code></pre>
  
  <p>And here's how I was attempting to call it in the controller:</p>
  
  <pre><code>public function testAction() {
      $response = $this-&gt;get('mysite.user.blog');
      return new Response($response);
  }
  </code></pre>
  
  <p>I did try using an event <a href="http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html" rel="noreferrer">subscriber/listener</a> tag, but that doesn't seem to accomplish the task I want.</p>
  
  <p>Here is my completely horrible attempt at a service. I couldn't get any response from it without using a constructor. </p>
  
  <pre><code>namespace MySite\SiteBundle\Services;
  
  use MySite\SiteBundle\Entity\Blog;
  
  class BlogUser {
  
      protected $entities;
  
      public function __construct(){
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $user = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
          $this-&gt;entities = $em-&gt;getRepository('MySiteBundle:Blog')-&gt;findBy(array('user' =&gt; $user));
      }
  }
  </code></pre>
  
  <p>Am I going about this the completely wrong way? Is there a better way that I'm missing?</p>
  
  <h1>EDIT/ANSWER:</h1>
  
  <p>modified my naming convention a little:</p>
  
  <pre><code>//services.yml
  mysite.user.blog.entities:
      class: Mysite\SiteBundle\Services\BlogUser
      arguments: ["@doctrine.orm.entity_manager", "@security.context"]
  </code></pre>
  
  <p>In the controller action:</p>
  
  <pre><code>$userEntities = $this-&gt;get('mysite.user.blog.entities');
  $entities = $userEntities-&gt;getEntities();
  </code></pre>
  
  <p>In the service itself:</p>
  
  <pre><code>class BlogUser {
  
      protected $entities;
  
      public function __construct($em, $securityContext){
          $user = $securityContext-&gt;getToken()-&gt;getUser();
          $this-&gt;entities = $em-&gt;getRepository('MySiteBundle:Blog')-&gt;findBy(array('user' =&gt; $user));
      }
      public function getEntities(){
          return $this-&gt;entities;
      }
  }
  </code></pre>
  
  <p>Still needs two lines to get the $entities variable in the controller, but this is way better than defining the same thing over and over.</p>