用于扩展实体的Doctrine EntityRepository

时间:2014-11-12 11:12:36

标签: symfony doctrine-orm

我遇到了将自定义实体存储库映射到从另一个实体扩展的实体的问题。

基础实体:

/**
 * User entity
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 *
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="user_type", type="string")
 * @ORM\DiscriminatorMap({"user" = "User", "client" = "Client"})
 */
class User
{
}

扩展实体:

/**
 * Client entity
 *
 * @ORM\Entity(repositoryClass="Acme\AppBundle\Entity\ClientRepository")
 */
class Client extends User
{
}

存储库:

namespace Acme\AppBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * ClientRepository
 */
class ClientRepository extends EntityRepository
{
    /*
     * this method returns resultset
     * its empty just for simplification
     */
    public function getClientsWithActiveCampaign(\DateTimeInterface $date = null) {}
}

调用存储库:

$clients = $this->getDoctrine()->getRepository('AcmeAppBundle:Client')->getClientsWithActiveCampaign();

但是当我在ClientRepository上调用自定义方法时,我得到了:

Undefined method 'getClientsWithActiveCampaign'. The method name must start with either findBy or findOneBy!

所以似乎Doctrine不知道我的自定义存储库。

1 个答案:

答案 0 :(得分:0)

考虑到这一点,实体经理失踪了,这就是它无法找到存储库的原因。在您的控制器/服务中,使用:

$em = $em= $this->getDoctrine()->getManager();
$clients = $em->getRepository('AcmeAppBundle:Client')->getClientsWithActiveCampaign(); 

现在应填充$ clients变量,否则至少会出现不同的错误。应该运行getClientsWithActiveCampaign函数。