Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 Class sub class of "Doctrine\ORM\EntityRepository" is not a valid entity or mapped super class

namespace griffin\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity(repositoryClass="griffin\UserBundle\Entity\UserRepository")
 * @ORM\Table(name="users")
 */
class User {

    const STATUS_ACTIVE   = 1;
    const STATUS_INACTIVE = 0;

    /**
     * @ORM\Id
     * @ORM\Column(name="id_users", type="smallint")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $idUsers;

And Repository class like

namespace griffin\UserBundle\Entity;
use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository {

    public function getAdmin()
    {
        return $this->getEntityManager()
                ->createQuery('select * from users where users_groups_id = 1')
                ->getResults;
    }
}

And when I call it in my controller

$results = $this->getDoctrine()
    ->getRepository('griffin\UserBundle\Entity\UserRepository')
    ->getAdmin();

var_dump($results);

I got error Class "griffin\UserBundle\Entity\UserRepository" sub class of "Doctrine\ORM\EntityRepository" is not a valid entity or mapped super class.

like image 922
kskaradzinski Avatar asked Apr 23 '13 09:04

kskaradzinski


2 Answers

getRepository() takes the entity class as first argument:

$results = $this->getDoctrine()
    ->getRepository('griffin\UserBundle\Entity\User')
    ->getAdmin();

Note: It's always a good idea to have a quick look at the EntityManager class itself. If you do, you will see this method signature for getRepository():

/**
 * Gets the repository for an entity class.
 *
 * @param string $entityName The name of the entity.
 *
 * @return EntityRepository The repository class.
 */
public function getRepository($entityName)
{
    //...
}
like image 100
Mick Avatar answered Nov 03 '22 01:11

Mick


Use $this->getDoctrine->getRepository('griffin\UserBundle\Entity\User') instead. The method getRepository expects the name of the entity you want the repository for, not the repository name itself, since not every entity has to have a custom repository class.

See http://symfony.com/doc/master/book/doctrine.html#custom-repository-classes

like image 37
thormeier Avatar answered Nov 03 '22 01:11

thormeier