SymBlog:未定义方法。方法名称必须以findBy或findOneBy开头 [英] SymBlog: Undefined method. The method name must start with either findBy or findOneBy

查看:362
本文介绍了SymBlog:未定义方法。方法名称必须以findBy或findOneBy开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与Symfony2的第4部分SymBlog项目
我收到此错误消息:

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

这是我的PostRepository类:

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();
    }

}

这是Controller的页面:

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
        ));
    }
...
}

我的../../../Entity/邮政编码:

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;
...
...

帖子 ScoRpion

这里的问题是什么?

推荐答案

不需要在这里使用存储库。如果你通过引用实体直接获取数据,那么你应该使用findBy或findOneBy由你的数据库字段名发布。
请尝试执行以下操作:

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
        ));
    }
...
}

这篇关于SymBlog:未定义方法。方法名称必须以findBy或findOneBy开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆