Symfony2 cookbook load user from DB tutorial - repository class missing

I am trying to follow the Symfony2 cookbook tutorial for loading users from a DB.

The tutorial assumes you have a ACME/UserBundle and my installation doesn't, but I just assumed I could make my own (it's not like a plugin-packade I need to download somewhere right?).

I created a bundle UserBundle and copy-pasted the code from the tutorial's entity User (first code box here).

This line seems to break things for me:

  @ORM\Entity(repositoryClass="Mycompany\UserBundle\Entity\UserRepository")

The error message I get is:

Fatal error: Class 'mycompany\UserBundle\Entity\UserRepository' not
found in /var/www/mycompany/vendor/doctrine/lib/Doctrine/ORM/EntityManager.php
on line 578

So I either assume I couldn't just create my own UserBundle (strange since I thought this was a tutorial on how to do it, not how to install a plugin that does it), or they assumed I knew that I somehow needed to register the entity among entityRepositories somehow?

I will be most grateful if anyone more senior in symfony would enlighten me on this. I truly love all that I have learned about Symfony2 so far, but I am a bit of a slow learner here.

Answers


It sounds like you dont have a user repository class, this is seperate to the user entity class. It goes in the entity folder but would be UserRepository.php and look something like:

namespace Mycompany\UserBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

// Implements userproviderinterface so we can use the user entity for authentication
// Extends entityrepository so that it gets methods definded there
class UserRepository extends EntityRepository implements UserProviderInterface {

  //  This function is called when a user tries to login, the below lets the user use their username or email for username
  public function loadUserByUsername($username) {
    $user = $this->createQueryBuilder('u')
            ->select('u, r')
            ->leftJoin('u.roles', 'r')
            ->where('u.username = :username OR u.email = :username')
            ->setParameter('username', $username)
            ->getQuery();
    try {
      $user = $user->getSingleResult();
    } catch (NoResultException $exc) {
      throw new UsernameNotFoundException(sprintf('Unable to find an active UserBundle:User object identified by %s', $username));
    }
    return $user;
  }
  // 
  public function refreshUser(UserInterface $user) {
    $class = get_class($user);
    if (!$this->supportsClass($class))
      throw new UnsupportedUserException(sprintf('instances of class %s are not supported', $class));
    return $this->loadUserByUsername($user->getUsername());
  }

  public function supportsClass($class) {
    return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
  }

}

This class is available futher down the tutorial you are doing http://symfony.com/doc/current/cookbook/security/entity_provider.html


You should be able to generate the correct class using the doctrine:generate:entities command. (Documented in the book.)

I think your command should look like this:

php app/console doctrine:generate:entities User

Need Your Help

Align text right justified but center of each box in geom_text

r ggplot2 geom-text

I have created a graph with ggplot using geom_tile and geom_text. I want to be able to read the numbers down one column and have them all lineup. I set the font to Courier and used hjust to justify...

Reading annotation property in aspect

java spring aop aspectj

How to read annotation property value in aspect?