无法获取自定义的存储库

编程入门 行业动态 更新时间:2024-10-19 14:47:22
本文介绍了无法获取自定义的存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在遵循Symfony2教程(第4章),但是我无法从我的自定义存储库中检索 getLatestBlogs 方法。

I'm following the Symfony2 tutorial (chapter 4), but I am having trouble retrieving the getLatestBlogs method from my custom repository.

我在Linux Mint上使用了Phar的Symfony 2.2。

I'm using Symfony 2.2 with Phar on Linux Mint.

我自己创建了这个存储库,但是我被骗了。我收到这个错误:

I created the repository myself, but I am stumped. I get this error:

未定义的方法'getLatestBlogs'。方法名必须以findBy或findOneBy开头! - BadMethodCallException

Undefined method 'getLatestBlogs'. The method name must start with either findBy or findOneBy! - BadMethodCallException

我已经搜索了其他类似的问题,但无济于事。

I have googled other similar questions but to no avail. Can anybody spot the error in my code?

附加信息

我的composer.json读取如下:

My composer.json reads as follows :

"require": { "php": ">=5.3.3", "symfony/symfony": "2.2.0", ** NOTE : Originally read 2.2.* but I changed and successfully ran a composer update ** "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.2.*", "twig/extensions": "1.0.*", "symfony/assetic-bundle": "2.1.*", "symfony/swiftmailer-bundle": "2.2.*", "symfony/monolog-bundle": "2.2.*", "sensio/distribution-bundle": "2.2.*", "sensio/framework-extra-bundle": "2.2.*", "sensio/generator-bundle": "2.2.*", "jms/security-extra-bundle": "1.4.*", "jms/di-extra-bundle": "1.3.*", "doctrine/doctrine-fixtures-bundle": "dev-master", "doctrine/data-fixtures" : "dev-master" },

我的 src / Blogger / BlogBu​​ndle / Controller / PageController.php :

namespace Blogger\BlogBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Blogger\BlogBundle\Entity\Enquiry; use Blogger\BlogBundle\Form\EnquiryType; class PageController extends Controller { public function indexAction() { $em = $this->getDoctrine() ->getManager(); $blogs = $em->getRepository('BloggerBlogBundle:Blog')->getLatestBlogs(); return $this->render('BloggerBlogBundle:Page:index.html.twig', array( 'blogs' => $blogs )); }

最初的谎言和我的 src /Blogger/BlogBu​​ndle/Entity/Blog.php :

namespace Blogger\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="blog") * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository") * @ORM\HasLifecycleCallbacks() */ class Blog {

,最后我的 src / Blogger / BlogBu​​ndle / Repository / BlogRepository.php :

namespace Blogger\BlogBundle\Repository; use Doctrine\ORM\EntityRepository; /** * BlogRepository * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ class BlogRepository extends EntityRepository { public function getLatestBlogs($limit = null) { $qb = $this->createQueryBuilder('b') ->select('b') ->addOrderBy('b.created', 'DESC'); if (false === is_null($limit)) $qb->setMaxResults($limit); return $qb->getQuery() ->getResult(); } }

推荐答案

您可以检查以下内容来解决此问题:

You can check the following to resolve this problem:

  • 确保您的注释中的FQCN与您的存储库文件,并且您设置了正确的命名空间。

  • Make sure your FQCN in your annotation matches your classname of the repository file and you have set the right namespace.

如果您有此活动或临时禁用Doctrine缓存,请清除所有元数据缓存。

Clear all metadata cache if you have this active or temporarily disable Doctrine caching.

app / console doctrine:cache:clear-metadata

如果您的配置中的映射类型设置为注释。即,如果您有 yml ,则必须在yml文件中定义 repositoryClass 。

Check if your mapping type is set to annotation in your configuration. i.e. if you have yml you have to define your repositoryClass in the yml file.

更多推荐

无法获取自定义的存储库

本文发布于:2023-11-28 16:44:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!