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.
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)
{
//...
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With