问题描述
我是 Symfony 2 和 Doctrine 的新手,有一个简单的问题:
I'm really new in Symfony 2 and Doctrine, and have a simple problem:
我的存储库中有一段非常简单的代码:
There is a pretty simple code in my repository:
<?php
namespace BakeryITBakeryBundleEntity;
use DoctrineORMEntityRepository;
class ProjectRepository extends EntityRepository
{
public function findHistory(){
return $this->getEntityManager()
->createQueryBuilder()
->select('p')
->from('Project','p')
->getQuery()
->getResult();
}
}
我的控制器中有两个简单的函数:
And two simple functions in my controller:
<?php
namespace BakeryITBakeryBundleController;
/*
...
*/
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,'Project p' 附近的第 14 列:错误:未定义类 'Project'.
This code throw me the Semantical Error [Semantical Error] line 0, col 14 near 'Project p': Error: Class 'Project' is not defined.
另一方面,如果我更改存储库中的这一行,则一切正常:
On the other hand everything works perfectly if I change this line in my repository:
->from('Project','p')
到
->from('BakeryITBakeryBundleEntityProject','p')
我不知道为什么这个例子在第一种情况下不起作用.我的 BakeryITBakeryBundleEntityProject 中的命名空间是这样设置的:
I don't know why this example doesn't work in the first case. Namespace in my BakeryITBakeryBundleEntityProject is set in this way:
namespace BakeryITBakeryBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Project
*
* @ORMTable()
* @ORMEntity(repositoryClass="BakeryITBakeryBundleEntityProjectRepository")
*/
class Project
{
/*
..
*/
}
推荐答案
为了使用简短形式,您还需要提供包名称.这是根据供应商和捆绑包名称构造的.在你的情况下,它会是这样的:
In order to use the short form, you'll need to provide the bundle name too. This is constructed from the vendor and bundle name. In your case it would be something like:
from('BakeryITBakeryBundle:Project')
有关捆绑包的更多信息,请参阅以下链接:
See the following link for more information on bundles:
http://symfony.com/doc/current/cookbook/bundles/best_practices.html
这篇关于Symfony2 Doctrine Custom Repository Class [Semantical Error] line 0, col 14 near 'Project p': Error: Class 'Project' is not defined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)