Symfony Bundle: how to avoid using __DIR__ to build the path to Doctrine mappings

∥☆過路亽.° 提交于 2020-01-05 03:39:12

问题


I'm finding myself dealing with the same problem in multiple bundles I've wrote.

The problem is that in my BundleNameBundle class I have to create the path to then load the mappings of Doctrine.

To do this I do something like:

/**
 * {@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());
}

Complete code here.

As you can see I use __DIR__ to get the path to the folder where the mappings are.

Now, Sensio Insights is alerting me that "Absolute path constants DIR and FILE should not be used".

Ok, but how can I solve this problem? Is there some alternative way to build the path to the mappings?


回答1:


You can use $this->path. It returns the same result as __DIR__



来源:https://stackoverflow.com/questions/42086485/symfony-bundle-how-to-avoid-using-dir-to-build-the-path-to-doctrine-mapping

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!