title: Symfony2 custom Voter: cannot have access to getDoctrine from inside the Voter
body: <p>I'm trying to implement a custom Voter.</p>
  
  <p>From the controller I call it this way:</p>
  
  <pre><code>$prj = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:Project')-&gt;findOneById($id);
  if (false === $this-&gt;get('security.authorization_checker')-&gt;isGranted('responsible', $prj)) {
      throw new AccessDeniedException('Unauthorised access!');
  }
  </code></pre>
  
  <p>The first line properly retrieves the Project object (I checked with a dump).</p>
  
  <p>The problem occurs inside the voter</p>
  
  <pre><code>&lt;?php
  namespace AppBundle\Security\Authorization\Voter;
  
  use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  use Symfony\Component\Security\Core\User\UserInterface;
  
  
  class ProjectVoter implements VoterInterface
  {
      const RESPONSIBLE = 'responsible';
      const ACCOUNTABLE = 'accountable';
      const SUPPORT = 'support';
      const CONSULTED = 'consulted';
      const INFORMED = 'informed';
  
      public function supportsAttribute($attribute)
      {
          return in_array($attribute, array(
              self::RESPONSIBLE,
              self::ACCOUNTABLE,
              self::SUPPORT,
              self::CONSULTED,
              self::INFORMED,
          ));
      }
  
      public function supportsClass($class)
      {
          $supportedClass = 'AppBundle\Entity\Project';
  
          return $supportedClass === $class || is_subclass_of($class, $supportedClass);
      }
  
      /**
       * @var \AppBundle\Entity\Project $project
       */
      public function vote(TokenInterface $token, $project, array $attributes)
      {
          // check if class of this object is supported by this voter
          if (!$this-&gt;supportsClass(get_class($project))) {
              return VoterInterface::ACCESS_ABSTAIN;
          }
  
          // check if the voter is used correct, only allow one attribute
          // this isn't a requirement, it's just one easy way for you to
          // design your voter
          if (1 !== count($attributes)) {
              throw new \InvalidArgumentException(
                  'Only one attribute is allowed'
              ); //in origin it was 'for VIEW or EDIT, which were the supported attributes
          }
  
          // set the attribute to check against
          $attribute = $attributes[0];
  
          // check if the given attribute is covered by this voter
          if (!$this-&gt;supportsAttribute($attribute)) {
              return VoterInterface::ACCESS_ABSTAIN;
          }
  
          // get current logged in user
          $user = $token-&gt;getUser();
  
          // make sure there is a user object (i.e. that the user is logged in)
          if (!$user instanceof UserInterface) {
              return VoterInterface::ACCESS_DENIED;
          }
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $projects = $em-&gt;getRepository('AppBundle:Project')-&gt;findPrjByUserAndRole($user, $attribute); 
  
          foreach ($projects as $key =&gt; $prj) {
              if ($prj['id'] === $project['id'])
                  {
                  $granted = true;
                  $index = $key; // save the index of the last time a specifif project changed status
                  }
              }
          if($projects[$index]['is_active']===true) //if the last status is active
              return VoterInterface::ACCESS_GRANTED;
          else
              return VoterInterface::ACCESS_DENIED;
      }
  }
  </code></pre>
  
  <p>I get the following error</p>
  
  <blockquote>
    <p>Attempted to call method "getDoctrine" on class
    "AppBundle\Security\Authorization\Voter\ProjectVoter".</p>
  </blockquote>
  
  <p>I understand that the controller extends Controller, that is why I can use "getDoctrine" there. How can I have access to my DB from inside the Voter?</p>