我正在关注 Symfony2 教程(第 4 章),但我无法getLatestBlogs
从我的自定义存储库中检索该方法。
我在 Linux Mint 上使用 Symfony 2.2 和 Phar。
我自己创建了存储库,但我很难过。我收到此错误:
未定义的方法“getLatestBlogs”。方法名称必须以 findBy 或 findOneBy 开头!- 坏方法调用异常
我用谷歌搜索了其他类似的问题,但无济于事。有人能在我的代码中发现错误吗?
附加信息
我的 composer.json 内容如下:
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.2.0", ** NOTE : Originally read 2.2.* but I changed and successfully ran a composer update **
"doctrine/orm": ">=2.2.3,<2.4-dev",
"doctrine/doctrine-bundle": "1.2.*",
"twig/extensions": "1.0.*",
"symfony/assetic-bundle": "2.1.*",
"symfony/swiftmailer-bundle": "2.2.*",
"symfony/monolog-bundle": "2.2.*",
"sensio/distribution-bundle": "2.2.*",
"sensio/framework-extra-bundle": "2.2.*",
"sensio/generator-bundle": "2.2.*",
"jms/security-extra-bundle": "1.4.*",
"jms/di-extra-bundle": "1.3.*",
"doctrine/doctrine-fixtures-bundle": "dev-master",
"doctrine/data-fixtures" : "dev-master"
},
我的src/Blogger/BlogBundle/Controller/PageController.php
:
namespace Blogger\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Blogger\BlogBundle\Entity\Enquiry;
use Blogger\BlogBundle\Form\EnquiryType;
class PageController extends Controller
{
public function indexAction()
{
$em = $this->getDoctrine()
->getManager();
$blogs = $em->getRepository('BloggerBlogBundle:Blog')->getLatestBlogs();
return $this->render('BloggerBlogBundle:Page:index.html.twig', array(
'blogs' => $blogs
));
}
原来是谎言和我的src/Blogger/BlogBundle/Entity/Blog.php
:
namespace Blogger\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="blog")
* @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Blog
{
最后是我的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();
}
}