【问题标题】:Can't call custom repository function in Symfony2无法在 Symfony2 中调用自定义存储库功能
【发布时间】:2012-11-14 02:59:12
【问题描述】:

我们在 Symfony2 项目中从控制器调用某个自定义实体存储库函数时遇到问题。我们之前已经成功地与其他实体合作过,所以我们可能遗漏了一些东西,我无法弄清楚它可能是什么。

我们的存储库类如下所示:

<?php

namespace OurSite\Bundle\OurBundle\Entity;
use Doctrine\ORM\EntityRepository;

class BlogRepository extends EntityRepository
{
    public function findPreviousPosts($limit = 6)
    {

        $q = $this->createQueryBuilder('q')
            ->where('q.category = :category')            
            ->setMaxResults($limit)                                                           
            ->add('orderBy', 'q.published ASC')
            ->getQuery();
        $res = $q->getResult();
        return $res;
    }
}

实体:

<?php

namespace OurSite\Bundle\OurBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
* OurSite\Bundle\OurBundle\Entity\Blog
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="OurSite\Bundle\OurBundle\Entity\BlogRepository")
*/
class Blog {
    // Non-relevant stuff here
}

当我们这样调用方法时:

$em = $this->getDoctrine()->getEntityManager();
$previousPosts = $em->getRepository('OurSiteOurBundle:Blog')->findPreviousPosts();

我们得到这个:

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

如果我们执行echo get_class($em-&gt;getRepository('OurSiteOurBundle:Blog'));,它会按预期输出BlogRepository

什么可能导致问题?我们在项目中有一个多余的bundle 目录,但我猜这不会导致它?

【问题讨论】:

  • 如果在方法名的find后面加上By然后调用呢?

标签: php symfony orm


【解决方案1】:

你以前用过这个实体吗?我看到博客的奇怪实体快捷方式

OurSiteOurBundle:博客

但您的博客有 OurSite\ Bundle\OurBundle\Entity 命名空间。我觉得应该是

OurSiteBundleOurBundle:博客

实体管理器将您指向错误的存储库类

【讨论】:

  • 实体之前已经使用过,但没有自定义存储库类方法。更改实体快捷方式会给出错误消息Unknown Entity namespace alias 'OurSiteBundleOurBundle'.
【解决方案2】:

如果您收到此错误:The method name must start with either findBy or findOneBy!,则表示您的自定义存储库未加载。

检查代码中的拼写错误,清除缓存,确保“OurSiteOurBundle”是实际的快捷方式名称。

【讨论】:

  • 我们检查并仔细检查了拼写错误并清除了几次缓存。我们还验证了快捷方式名称匹配 - 如果我们在类名称中添加拼写错误,例如 'OurSiteOurBundle:Blogxxx',我们会收到“找不到类”错误。
  • @Kaivosukeltaja 你能在findPreviousPosts 方法的第一行添加一个echo 'test';exit;,看看它是否被打印出来?还可以尝试将方法重命名为 getPreviousPosts
  • echoed 没有。将方法名称更改为getPreviousPosts 也没有帮助,错误消息只是更改为Undefined method 'getPreviousPosts'.
  • 所以你有它,你的自定义存储库类没有加载。由于这是一项经过充分测试并适用于所有版本的功能,因此我建议重新检查是否存在拼写错误或错误配置。
  • 但请注意,存储库的get_class() 返回BlogRepository。如果自定义存储库无法加载,我认为应该是EntityRepository
【解决方案3】:

根据您提供的来源,这可能不是您的问题,但它可能会为其他人节省一些搜索时间。

我遇到了同样的“必须以 findBy 或...开头”错误,结果在我的实体定义中我不小心两次调用了 @ORM\Entity 注释。第一次我正确使用它并设置了repositoryClass,但第二次我只是单独使用它(就像没有自定义存储库的实体一样),所以覆盖了以前的repositoryClass定义。

 /**
 *
 * @ORM\Entity(repositoryClass="Company\TestBundle\Entity\MyEntityRepository")
 * @ORM\Table(name="testing_my_entity")
 * @ORM\Entity
 */

class MyEntity
{
etc...
}

【讨论】:

    【解决方案4】:

    我遇到了同样的问题。我看过很多关于这个的帖子,但都没有解决它。

    最后我发现这是因为我之前使用的是生成的yml文件,所以Doctrine没有读取注解进行映射!

    所以请确保您没有任何 yml/xml Doctrine 文件。

    然后:

    app/console doctrine:cache:clear-metadata
    

    【讨论】:

      【解决方案5】:

      如果使用 xml 进行映射(通过测试): 更新xml或yml映射文件,添加repository-class属性:

      <entity name="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContent" table="uv_update_page_content" **repository-class="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContentRepository"**>
      

      http://doctrine-mongodb-odm.readthedocs.org/en/latest/cookbook/mapping-classes-to-orm-and-odm.html 然后更新学说缓存:

      php app/console doctrine:cache:clear-metadata
      

      使用 yml(未测试):

      Acme\DemoBundle\Entity\Post:
        type: entity
        table: posts
        RepositoryClass: Acme\DemoBundle\Entity\PostRepository 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        • 2013-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-09
        • 1970-01-01
        相关资源
        最近更新 更多