Symfony2 Doctrine Custom Repository Class [Semantical Error]


Symfony2 Doctrine Custom Repository Class [Semantical Error] line 0, col 14 near 'Project p': Error: Class 'Project' is not defined

我对Symfony 2和Doctrine真的很陌生,有一个简单的问题:

我的存储库中有一个非常简单的代码:

<?php
namespace BakeryIT'BakeryBundle'Entity;
use Doctrine'ORM'EntityRepository;
class ProjectRepository extends EntityRepository
{
    public function findHistory(){
        return $this->getEntityManager()
        ->createQueryBuilder()
        ->select('p')
        ->from('Project','p')
        ->getQuery()
        ->getResult();
    }
}

我的控制器中有两个简单的功能:

<?php
namespace BakeryIT'BakeryBundle'Controller;
/*
...
*/
class ProjectController extends Controller
{
    public function indexAction()
    {
        return $this->index('Project', 'findHistory');
    }
}

控制器看起来像这样:

public function index($entity, $query = 'findAll')
{
    $repository = $this->getDoctrine()
        ->getRepository('BakeryBundle:'.$entity);
    $data = $repository->$query();
    return $this->render('BakeryBundle:'.$entity.':index.html.twig',
    array('data' => $data));
}

这段代码向我抛出了语义错误 [语义错误] 第 0 行,第 14 行,靠近"项目 p":错误:未定义类"项目"。

另一方面,如果我在我的存储库中更改这一行,一切都可以完美运行:

->from('Project','p')

->from('BakeryIT'BakeryBundle'Entity'Project','p')

我不知道为什么这个例子在第一种情况下不起作用。我的 BakeryIT''BakeryBundle''Entity''Project 中的命名空间是这样设置的:

namespace BakeryIT'BakeryBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * Project
 *
 * @ORM'Table()
 * @ORM'Entity(repositoryClass="BakeryIT'BakeryBundle'Entity'ProjectRepository")
 */
class Project
{
/*
..
*/
}

为了使用缩写形式,您还需要提供捆绑包名称。这是根据供应商和捆绑名称构造的。在您的情况下,它将是这样的:

from('BakeryITBakeryBundle:Project')

有关捆绑包的更多信息,请参阅以下链接:

http://symfony.com/doc/current/cookbook/bundles/best_practices.html

在我的本地系统上,我可以使用

$entityManager->createQuery('DELETE FROM BellaShopBundle:cache c WHERE c.expires < :now')->setParameter('now', $date);

但是在生产中,有必要将实体或类名大写,我想我将其视为表名,因此:

$entityManager->createQuery('DELETE FROM BellaShopBundle:Cache c WHERE c.expires < :now')->setParameter('now', $date);