Aerendir Aerendir - 4 years ago 158
PHP Question

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

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

The problem is that in my

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)

$modelDir = realpath(__DIR__ . '/Resources/config/doctrine/mappings');
$mappings = [
$modelDir => 'SerendipityHQ\Bundle\QueuesBundle\Model',

$ormCompilerClass = DoctrineOrmMappingsPass::class;
if (class_exists($ormCompilerClass)) {

$container->addCompilerPass(new DaemonDependenciesPass());

Complete code here.

As you can see I use
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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download