Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

The method name must start with either findBy or findOneBy. Undefined method Symfony?

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 BloggerBlogBundleRepository;
use DoctrineORMEntityRepository;

/**
 * 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
    ));
    }

    // ..
}

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 BloggerBlogBundleEntity;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass="BloggerBlogBundleRepositoryBlogRepository")
 * @ORMTable(name="blog")
 * @ORMHasLifecycleCallbacks()
 * @ORMEntity
 */

class Blog
 {
  /**
   * @ORMId
   * @ORMColumn(type="integer")
   * @ORMGeneratedValue(strategy="AUTO")
   * @ORMHasLifecycleCallbacks()
  */
  protected $id;
  --
  --
 }

Where Am I doing wrong ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure that you have modified your entity class:

// src/Blogger/BlogBundle/Entity/Blog.php
/**
 * @ORMEntity(repositoryClass="BloggerBlogBundleRepositoryBlogRepository")
 * @ORMTable(name="blog")
 * @ORMHasLifecycleCallbacks()
 */
class Blog
{
    // ..
}

the annotation @ORMEntity(repositoryClass="BloggerBlogBundleRepositoryBlogRepository") is required.

And don't forget to regenerate entities:

php app/console doctrine:generate:entities Blogger

UPDATE

Remove annotation @ORMEntity. It overrides correct annotation @ORMEntity(repositoryClass="BloggerBlogBundleRepositoryBlogRepository")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...