I work through part4 of Symfony2 , and when updating the controller code and helper class I received the following error message
Undefined method 'getLatestBlogs'. The method name must start with either findBy or findOneBy!
before I put some code in the controller, which I moved to my helper class, as described in the tutorial, resulting in an 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 )); } // .. }
I am attaching a few lines from the /Entity/Blog.php file. see how correct they are according to 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; -- -- }
Where am I doing wrong?
symfony
ScoRpion Feb 07 2018-12-12T00: 00Z
source share