gpt4 book ai didi

symfony2 类 "Doctrine\ORM\EntityRepository"的子类不是有效的实体或映射的父类(super class)

转载 作者:行者123 更新时间:2023-12-04 02:23:42 24 4
gpt4 key购买 nike

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;

和 Repository 类
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;
}
}

当我在 Controller 中调用它时
$results = $this->getDoctrine()
->getRepository('griffin\UserBundle\Entity\UserRepository')
->getAdmin();

var_dump($results);

我有错误 Class "griffin\UserBundle\Entity\UserRepository" sub class of "Doctrine\ORM\EntityRepository" is not a valid entity or mapped super class.

最佳答案

getRepository()将实体类作为第一个参数:

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

注:快速浏览一下 EntityManager 总是一个好主意。类本身。如果这样做,您将看到 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)
{
//...
}

关于symfony2 类 "Doctrine\ORM\EntityRepository"的子类不是有效的实体或映射的父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16165272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com