symfony2和doctrine2较短的实体名称 [英] symfony2 and doctrine2 shorter entity names
问题描述
$ dql =从MyCompanyMySuperPuperBundle中选择i
:问题i
内加入MyCompanyMySuperPuperBundle:跟踪t与t.id = i.tracker
其中t.name在(?1)和i.version =?2;
而不是
code> $ dql =select i
from Issue i
inner join Tracker t with t.id = i.tracker
其中t.name in(?1)和i。 version =?2;
全部代码:
命名空间MyCompany\MySuperPuperBundle\Entity;
使用Doctrine\ORM\EntityRepository;
class IssueRepository扩展EntityRepository
{
public function findStoriesByVersion(\MyCompany\MySuperPuperBundle\Entity\Version $ version)
{
$ dql =从MyCompanyMySuperPuperBundle中选择i
:问题i
内部连接MyCompanyMySuperPuperBundle:跟踪t与t.id = i.tracker
其中t.name在(?1)和i.version = ?2\" ;
return $ this-> getEntityManager()
- > createQuery($ dql)
- > setParameter(1,array('Epic','Story' 'Spike','Extra'))
- > setParameter(2,$ version-> getId())
- > getResult();
}
}
更新: p>
似乎没有办法设置默认每个包前缀,我必须使用所有实体的愚蠢前缀...超过200个实体...好的..允许设置别名。
它是通过以下方式完成的:
orm:
auto_generate_proxy_classes:%kernel.debug%
entity_managers:
默认值:
映射:
MyCompanyMySuperPuperBundle:
类型:注释
别名:xr
#auto_mapping:true
现在我可以使用xr作为前缀
$ dql =select i
from xr:Issue i
inner join xr:Tracker t with t.id = i.tracker
where t.name in(?1)and i .version =?2;
但现在细枝末节告诉我
在MyCompanyMySuperPuperBundle中的模板渲染(未知实体命名空间别名UMyCompanyMySuperPuperBundle。)期间抛出异常。默认值:index.html.twig在第7行。
我不能使用xr前缀的twig - 它不工作。你有什么想法吗?
PS:如果我可以在一个代码中使用两个别名,那将是完美的MyCompanyMySuperPuperBundle - full,and xr - short ...
更新:已解决
它的工作原理!现在我可以通过defautl全名和很短的名字访问模型。
twig使用长命名空间名称,因此它可以工作。
class MyCompanyMySuperPuperBundle extends Bundle
{
public function boot()
{
//实现基本命名空间的别名XR
$ em = $ this-> container-> get(doctrine.orm.entity_manager);
$ config = $ em-> getConfiguration();
$ config-> addEntityNamespace(XR,MyCompany\\MySuperPuperBundle\\\Entity);
}
}
在Doctrine中无法使用默认命名空间,除非您的实体本身没有命名空间。你可以做的是指定一个较短的命名空间,像
$ em = $ container-> get(' doctrine.orm.entity_manager');
$ config = $ em-> getConfiguration();
$ config-> addEntityNamespace('e','MyCompany\\Bundle\\\Entity');
之后,您可以将您的实体称为e:问题。你可以把它放在预先请求的事件监听器中,或者你的bundle的boot()方法中。
Who to get rid of using namespaces in DQL queries? I want to assign default namespace for all doctrine requests from my bundle. It will be perfect to use default namespace in query builder too. I would like to have:
$dql = "select i
from MyCompanyMySuperPuperBundle:Issue i
inner join MyCompanyMySuperPuperBundle:Tracker t with t.id = i.tracker
where t.name in (?1) and i.version = ?2";
instead of
$dql = "select i
from Issue i
inner join Tracker t with t.id = i.tracker
where t.name in (?1) and i.version = ?2";
Full code:
namespace MyCompany\MySuperPuperBundle\Entity;
use Doctrine\ORM\EntityRepository;
class IssueRepository extends EntityRepository
{
public function findStoriesByVersion(\MyCompany\MySuperPuperBundle\Entity\Version $version)
{
$dql = "select i
from MyCompanyMySuperPuperBundle:Issue i
inner join MyCompanyMySuperPuperBundle:Tracker t with t.id = i.tracker
where t.name in (?1) and i.version = ?2";
return $this->getEntityManager()
->createQuery($dql)
->setParameter(1, array('Epic', 'Story', 'Spike', 'Extra'))
->setParameter(2, $version->getId())
->getResult();
}
}
UPDATE:
It seems like there is no way to setup default per bundle prefix and i have to use that stupid prefixes for all entities... more than 200 entities... ok.. lets setup alias. it was done via:
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
mappings:
MyCompanyMySuperPuperBundle:
type: annotation
alias: xr
#auto_mapping: true
Now i can use xr as prefix
$dql = "select i
from xr:Issue i
inner join xr:Tracker t with t.id = i.tracker
where t.name in (?1) and i.version = ?2";
But now twig telling me that
An exception has been thrown during the rendering of a template ("Unknown Entity namespace alias 'UMyCompanyMySuperPuperBundle'.") in MyCompanyMySuperPuperBundle:Default:index.html.twig at line 7.
And i can't use xr prefix for twig - it doesn't work. Do you have any ideas?
PS: It will perfect if i can use both aliases in one code MyCompanyMySuperPuperBundle - full, and xr - short...
UPDATE: RESOLVED
It works! Now i can access to models via defautl full name and very short name. twig's using long namespace name so it works.
class MyCompanyMySuperPuperBundle extends Bundle
{
public function boot()
{
// implement alias XR for base namespace
$em = $this->container->get("doctrine.orm.entity_manager");
$config = $em->getConfiguration();
$config->addEntityNamespace("XR", "MyCompany\\MySuperPuperBundle\\Entity");
}
}
There is no way to have a default namespace in Doctrine, unless your entities themselves have no namespace at all. What you can do however, is specify a shorter namespace, with something like
$em = $container->get('doctrine.orm.entity_manager');
$config = $em->getConfiguration();
$config->addEntityNamespace('e', 'MyCompany\\Bundle\\Entity');
After that, you can refer to your entities as "e:Issue". You can put this into a pre-request event listener, or your bundle's boot() method.
这篇关于symfony2和doctrine2较短的实体名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!