-1

I am currently working with Symfony2's Part 4 OF The SymBlog project I am getting this ERROR message:

Undefined method 'getLatestPosts'. The method name must start with either findBy 
or findOneBy!500 Internal Server Error - BadMethodCallException

This is my PostRepository Class:

    <?php

namespace BLog\BlogBundle\Entity; use Doctrine\ORM\EntityRepository;

class PostRepository extends EntityRepository {

    public function getLatestPosts($limit = null) {
        $qp = $this->createQueryBuilder('p')
                ->select('p')
                ->addOrderBy('p.created', 'DESC');

        if (false === is_null($limit)) {
            $qp->setMaxResults($limit);
        }


        return $qp->getQuery()
                        ->getResult();
    }

}

This is the Controller's page Action method:

<?php

namespace Blog\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller {

    public function indexAction() {
        $em = $this->getDoctrine()
                ->getEntityManager();

        $posts = $em->getRepository('BlogBundle:Post')
                ->getLatestPosts();

        return $this->render('BlogBundle:Default:home.html.twig', > >array(
                    'posts' => $posts
        ));
    }
...
}

This is a sample of my ../../../Entity/Post code:

<?php

namespace Blog\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="Blog\BlogBundle\Entity\PostRepository")
 * @ORM\Table(name="post")
 * @ORM\HasLifecycleCallbacks
 */

class Post {


....
...
..
/**
     * @ORM\Column(type="text")
     */
    protected $post;
...
...

I also tried all solutions in this post by ScoRpion

What is THE PROBLEM here ???

Community
  • 1
  • 1
K-Alex
  • 380
  • 1
  • 3
  • 17
  • all looks good so it should work. You have a typo in ``namespace BLog\BlogBundle\Entity`` (capital ``L`` in ``BLog``). It should make no difference but try to change it – Tomasz Madeyski Mar 05 '15 at 08:43
  • Thank you @ Tomasz Madeysk for your response.. still, the problem exists .... I tried forwarding the getLatestPosts() Method, But it returned nothing – K-Alex Mar 05 '15 at 11:11
  • possible duplicate of [SymBlog: Undefined method. The method name must start with either findBy or findOneBy](http://stackoverflow.com/questions/28852157/symblog-undefined-method-the-method-name-must-start-with-either-findby-or-find) – gp_sflover Apr 30 '15 at 21:43

4 Answers4

3

Check out this:

$posts = $em->getRepository('BlogBundle:Post')
                ->getLatestPosts();

It must be

$posts = $em->getRepository('BlogBlogBundle:Post')
                ->getLatestPosts();

Look up your namespace.

Codew
  • 474
  • 4
  • 16
2

Removing the full path to Repository from the Entity class annotation works for me : @ORM\Entity(repositoryClass="AdvertRepository")
I don't understand why though...

ngduflo
  • 31
  • 2
1

For me the solution was to put only the name of the repository, without the full path to it.

Was (error):

* @ORM\Entity(repositoryClass="OC\PlatformBundle\Repository\AdvertRepository")

Should be (working):

* @ORM\Entity(repositoryClass="AdvertRepository")
Oksana
  • 33
  • 1
  • 6
-2

The solution was to put the repository class inside the Entity directory next to the Post Entity and this is the Entity class now:

<?php

namespace Blog\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="PostRepository")
 * @ORM\Table(name="post")
 * @ORM\HasLifecycleCallbacks
 */

class Post {


....
...
..
/**
     * @ORM\Column(type="text")
     */
    protected $post;
...
...
K-Alex
  • 380
  • 1
  • 3
  • 17