如何在symfony2中创建自定义存储库

时间:2013-10-21 19:29:32

标签: symfony

用户实体:     

namespace Core\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 * @ORM\Entity(repositoryClass="Core\UserBundle\Entity\UserRepository")
 * @ORM\Table(name="user")
 */
class User
{
}

用户存储库:

<?php 
# src/Core/UserBundle/Entity/UserRepository.php

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

class UserRepository extends EntityRepository
{

    public function example()
    {
        return "hello word";
    }
}

控制器:

$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('CoreUserBundle:User');
$repo->example();

注意:get_class($repo) => Doctrine\ORM\EntityRepository

错误:

Error Message : Undefined method 'example'. The method name must start with either findBy or findOneBy!

1 个答案:

答案 0 :(得分:0)

试试这个

$ repo = $ em-&gt; getRepository(&#39; UserBundle:User&#39;);