Home » Php » doctrine orm – autloading not working correct using autoloader.php in vendor directory

doctrine orm – autloading not working correct using autoloader.php in vendor directory

Posted by: admin July 12, 2020 Leave a comment

Questions:

i’ve troube with the autoloading of composer as the autoloader can’t resolve Doctrine\ORM\Mapping\Table.
For the Unittests i have created doctrine entity classes with typical Annotations:

<?php

namespace OmniSearchTest\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Picture
 *
 * @ORM\Table(name="picture")
 * @ORM\Entity
 */
class Picture
{

and created a new entity manager by using this entities. But im getting The Message:

Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Doctrine\ORM\Mapping\Table" in class OmniSearchTest\Entity\Picture does not exist, or could not be auto-loaded.

For some Unittests

First, i have the following project structure:

/src
    /OmniSearch
        SomeClass.php
/tests
    /OmniSearchTest
        SomeClassTest.php
/composer.json
/phpunit.xml.dist

My composer.json looks like this:

{
    /* ... */

    "require": {
        "php": ">=5.4",
        "doctrine/orm": "2.*"
    },
    "require-dev": {
        "phpunit/phpunit": "4.*"
    },
    "autoload": {
        "psr-0": {
            "OmniSearch\": "src/"
        }
    },
    "autoload-dev": {
        "psr-0": {
            "OmniSearchTest\": "tests/"
        }
    }
}

While my phpunit looks excactly like this:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="vendor/autoload.php"
     strict="true"
     verbose="true">
    <testsuites>
        <testsuite name="omnisearch">
            <directory>./tests/OmniSearchTest</directory>
        </testsuite>
    </testsuites>
</phpunit>

I cutted off this project from another zf2 project of mine where the autoloading was working fine. Im not sure what exactly went wrong because the autogenerated autoload_namespaces.php contains the the mapping:

'Doctrine\ORM\' => array($vendorDir . '/doctrine/orm/lib'),
How to&Answers:

This is kind of a shot in the dark but Symfony 2 applications include an autoload.php file which explicitly loads an annotation registry.

// autoload.php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/**
 * @var ClassLoader $loader
 */
$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;

I never really researched why in any detail since I don’t use annotations. But give it a try. Can’t hurt.

Answer:

This is a bit old, but I created a composer plugin which registers composer ClassLoader into the AnnotationRegistry as a loader.

https://github.com/indigophp/doctrine-annotation-autoload

Answer:

First run this command:

composer require indigophp/doctrine-annotation-autoload

After that finishes, run this:

composer dump-autoload