symfony 2 - 尝试调用类"testBundle\Entity\test"的名为"findBy"的未定义方法

cnm*_*cha 1 php symfony doctrine-orm

我无法从数据库中获取数据.这是代码:

$testRepository= $this->getDoctrine()->getRepository('testBundle:test');
    $potds = $testRepository->findBy(
        array(),
    );
Run Code Online (Sandbox Code Playgroud)

这是实体类:

 * @ORM\Table()
 * @ORM\Entity(repositoryClass="testBundle\Entity\test")
 * @ORM\HasLifecycleCallbacks
 */
class test
{ /* ... */ }
Run Code Online (Sandbox Code Playgroud)

这是存储库的代码:

namespace testBundle\Entity;

/**
 * TestRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class testRepository extends \Doctrine\ORM\EntityRepository
{
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:symfony 2 - 尝试调用类"testBundle\Entity\test"的名为"findBy"的未定义方法

使用Doctrine插入数据是有效的,因此实体应该有一切正确.

Kam*_*nek 5

您的存储库类定义不正确:

@ORM\Entity(repositoryClass="testBundle\Entity\test")
Run Code Online (Sandbox Code Playgroud)

应该:

@ORM\Entity(repositoryClass="testBundle\Entity\testRepository")
Run Code Online (Sandbox Code Playgroud)

现在这段代码:

$testRepository= $this->getDoctrine()->getRepository('testBundle:test');
Run Code Online (Sandbox Code Playgroud)

是返回实体类,而不是存储库.