【问题标题】:symfony 2 - Attempted to call an undefined method named "findBy" of class "testBundle\Entity\test"symfony 2 - 试图调用类“testBundle\Entity\test”的名为“findBy”的未定义方法
【发布时间】:2016-08-17 08:40:51
【问题描述】:

我无法从数据库中获取数据。代码如下:

$testRepository= $this->getDoctrine()->getRepository('testBundle:test');
    $potds = $testRepository->findBy(
        array(),
    );

这是实体类:

 * @ORM\Table()
 * @ORM\Entity(repositoryClass="testBundle\Entity\test")
 * @ORM\HasLifecycleCallbacks
 */
class test
{ /* ... */ }

这是存储库的代码:

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
{
}

我收到此错误消息:symfony 2 - Attempted to call an undefined method named "findBy" of class "testBundle\Entity\test"

使用 Doctrine 插入数据是可行的,因此实体应该一切正常。

【问题讨论】:

  • 请提供Repository的代码和路径

标签: php symfony doctrine-orm


【解决方案1】:

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

@ORM\Entity(repositoryClass="testBundle\Entity\test")

应该是:

@ORM\Entity(repositoryClass="testBundle\Entity\testRepository")

现在这段代码:

$testRepository= $this->getDoctrine()->getRepository('testBundle:test');

正在返回实体类,而不是存储库。

【讨论】:

    【解决方案2】:

    您对 repositoryClass 的定义是错误的

    @ORM\Entity(repositoryClass="testBundle\Entity\test")
    

    不提供完整路径和类名。

    @ORM\Entity(repositoryClass="testBundle\Entity\testRepository")
    

    您需要提供完整的路径和类名,以便 Symfony 能够获得正确的路由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多