【问题标题】:Symfony2 Doctrine Custom Repository Class [Semantical Error] line 0, col 14 near 'Project p': Error: Class 'Project' is not definedSymfony2 Doctrine Custom Repository Class [Semantical Error] line 0, col 14 near 'Project p': Error: Class 'Project' is not defined
【发布时间】:2014-03-05 22:34:09
【问题描述】:

我是 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));
}

此代码向我抛出语义错误[Semantical Error] line 0, col 14 near 'Project p': Error: Class 'Project' is not defined.

另一方面,如果我在存储库中更改此行,一切都会完美运行:

->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
{
/*
..
*/
}

【问题讨论】:

    标签: php symfony doctrine-orm


    【解决方案1】:

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

    from('BakeryITBakeryBundle:Project')
    

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

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

    【讨论】:

      【解决方案2】:

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

      $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);
      

      【讨论】:

        猜你喜欢
        • 2016-05-23
        • 1970-01-01
        • 2014-08-06
        • 2022-11-03
        • 1970-01-01
        • 2017-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多