0

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

1 Answers1

0

No need to use repository here. If you are fetching data directly by refering entity then you should use findBy or findOneBy posted by your database field name. Please Try to do following way:

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

class DefaultController extends Controller {

    public function indexAction() {

        $posts = this->forward('BlogBundle:Post:getLatestPosts', array(), array());

        return $this->render('BlogBundle:Default:home.html.twig', > >array(
                    'posts' => $posts
        ));
    }
...
}
Sachin I
  • 1,500
  • 3
  • 10
  • 29
  • @ user4631643 It returns nothing, I var dumped the $posts variable, It printed the following::: The _controller value "BlogBundle:Post:getLatestPosts" maps to a "Blog\BlogBundle\Controller\PostController" class, but this class was not found. Create this class or check the spelling of the class and its namespace. – K-Alex Mar 04 '15 at 13:14