with doctrine2 ,我在 Entities
文件夹中创建了我的 entities
。
文件夹:
./
HelloEntity.php
WorldEntity.php
我在PHP中创建了我的结构并导入它们:
$namespaceYaml = array($connection->getBundle()->getNamespace() => $bundleFolder.'/Entity/ORM/');
$driver = new YamlDriver($namespaceYaml, '.orm.yml');
$path = $bundleFolder.'/Resource/config/doctrine/metadata/orm';
$config = Setup::createYAMLMetadataConfiguration(array($path), true);
$config->setEntityNamespaces(array($connection->getBundle()->getName() => $connection->getBundle()->getNamespace().'Entity'));
$config->setMetadataDriverImpl($driver);
现在,它正在工作。我可以使用它们。
例如:
$qb = $this->getRepository()->createQueryBuilder('Hello'); //short here but long in from()
$qb->from('MyWebEntitiesHello', 'h'); //Hello Entity
现在,MyWeb
是我的主要根/名称空间。当我想在桌子上使用学说时,我必须写MyWebEntitiesHello
。我要做的就是缩短它们。我不能使用'Hello'
有人有任何想法吗?
从未尝试过用纯PHP尝试Doctrine2,但该行看起来像自动加载器配置:
$config->setEntityNamespaces(array($connection->getBundle()->getName() => $connection->getBundle()->getNamespace().'Entity'));
也许如果您使用目录/名称空间结构,例如MyWeb entity hello hello hello,它将按照您想要
目前,我在stackoverflow上的其他地方找到了答案。这不是同一个问题,但看起来好像回答了。
很快,我们必须使用完全合格的名称空间来代替简短名称。但是,当我们获得存储库时,我们可以使用简短名称(除了捆绑名称)
参考:Deotrine DQL和名称空间(仅相对?)
现在,我找到了答案。
答案:
我以前的解决方案是正确的。我们必须在第一个str。
但是,我们也可以简短地使用它。
参见,每个entityManmager
都有一个Repository
,我们可以使用$em->getRepository()
来获得它。之后,使用createQueryBuilder('e')
(注意 e "此处)生成HelloEntity
本身并将其分配给e
别名。
所以,我的代码是错误的,无需输入名称空间:)
结论:
而不是这样:
$qb = $this->getRepository()->createQueryBuilder('Hello'); //short here but long in from()
$qb->from('MyWebEntitiesHello', 'h'); //Hello Entity
使用此:
$qb = $this->getRepository()->createQueryBuilder('h'); //h assigned for Hello table and HelloEntity. So no need for from. It will fetch data two times then.
已解决。
修复和想法来自:Doctrine2获取每个记录的5副本