Symfony Bundle:如何避免使用 __DIR__ 来构建 Doctrine 映射的路径
Symfony Bundle: how to avoid using __DIR__ to build the path to Doctrine mappings
我发现自己在我编写的多个包中都遇到了同样的问题。
问题是在我的 BundleNameBundle
class 中我必须创建路径然后加载 Doctrine 的映射。
为此我做了类似的事情:
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
$modelDir = realpath(__DIR__ . '/Resources/config/doctrine/mappings');
$mappings = [
$modelDir => 'SerendipityHQ\Bundle\QueuesBundle\Model',
];
$ormCompilerClass = DoctrineOrmMappingsPass::class;
if (class_exists($ormCompilerClass)) {
$container->addCompilerPass(
$this->getYamlMappingDriver($mappings)
);
}
$container->addCompilerPass(new DaemonDependenciesPass());
}
完整代码here.
如您所见,我使用 __DIR__
获取映射所在文件夹的路径。
现在,Sensio Insights 提醒我“Absolute path constants DIR and FILE should not be used”。
好的,但是我该如何解决这个问题呢?是否有其他方法来构建映射路径?
您可以使用 $this->path
。它 returns 与 __DIR__
相同的结果
我发现自己在我编写的多个包中都遇到了同样的问题。
问题是在我的 BundleNameBundle
class 中我必须创建路径然后加载 Doctrine 的映射。
为此我做了类似的事情:
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
$modelDir = realpath(__DIR__ . '/Resources/config/doctrine/mappings');
$mappings = [
$modelDir => 'SerendipityHQ\Bundle\QueuesBundle\Model',
];
$ormCompilerClass = DoctrineOrmMappingsPass::class;
if (class_exists($ormCompilerClass)) {
$container->addCompilerPass(
$this->getYamlMappingDriver($mappings)
);
}
$container->addCompilerPass(new DaemonDependenciesPass());
}
完整代码here.
如您所见,我使用 __DIR__
获取映射所在文件夹的路径。
现在,Sensio Insights 提醒我“Absolute path constants DIR and FILE should not be used”。
好的,但是我该如何解决这个问题呢?是否有其他方法来构建映射路径?
您可以使用 $this->path
。它 returns 与 __DIR__