方法名称必须以 findBy 或 findOneBy 开头.未定义的方法 Symfony? [英] The method name must start with either findBy or findOneBy. Undefined method Symfony?
问题描述
我正在研究 Symfony2 的第 4 部分,在更新控制器和辅助类代码时,我收到以下错误消息
I am working through part4 of Symfony2, and while updating the controller and helper class code i got the following error message
Undefined method 'getLatestBlogs'. The method name must start with either
findBy or findOneBy!
在我将一些代码放入控制器之前,我按照教程的教导转移到了我的助手类,这导致了上述错误消息.
before i had put some code in controller that i shifted to my helper class as taught by tutorial, which result in the above error message.
<?php
// src/Blogger/BlogBundle/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();
}
}
这是我的控制器文件索引操作代码:-
And here is my controller file index Action Code:-
// src/Blogger/BlogBundle/Controller/PageController.php
class PageController extends Controller
{
public function indexAction()
{
$em = $this->getDoctrine()
->getEntityManager();
$blogs = $em->getRepository('BloggerBlogBundle:Blog')
->getLatestBlogs();
return $this->render('BloggerBlogBundle:Page:index.html.twig', array(
'blogs' => $blogs
));
}
// ..
}
我从/Entity/Blog.php 文件中附上几行.请根据您的回答查看它们是否正确.
I am attaching few lines from /Entity/Blog.php file. please see if they are correct as per your answer.
<?php
// src/Blogger/BlogBundle/Entity/Blog.php
namespace Blogger\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
* @ORM\Table(name="blog")
* @ORM\HasLifecycleCallbacks()
* @ORM\Entity
*/
class Blog
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\HasLifecycleCallbacks()
*/
protected $id;
--
--
}
我哪里做错了?
推荐答案
确保你已经修改了你的实体类:
Make sure that you have modified your entity class:
// src/Blogger/BlogBundle/Entity/Blog.php
/**
* @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
* @ORM\Table(name="blog")
* @ORM\HasLifecycleCallbacks()
*/
class Blog
{
// ..
}
注释 @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
是必需的.
the annotation @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
is required.
并且不要忘记重新生成实体:
And don't forget to regenerate entities:
php app/console doctrine:generate:entities Blogger
更新
移除注解@ORM\Entity
.它覆盖了正确的注释 @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
UPDATE
Remove annotation @ORM\Entity
. It overrides correct annotation @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
这篇关于方法名称必须以 findBy 或 findOneBy 开头.未定义的方法 Symfony?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!