Skip to content

Instantly share code, notes, and snippets.

@gtnsimon
Last active February 26, 2024 12:14
Show Gist options
  • Save gtnsimon/8a657e0a29e674677a2c65c0ba07deff to your computer and use it in GitHub Desktop.
Save gtnsimon/8a657e0a29e674677a2c65c0ba07deff to your computer and use it in GitHub Desktop.
Doctrine ORM + Symfony Validator + Swagger-PHP annotations living together πŸ™Œβ€

This solve Exceptions below :

  • Doctrine\Common\Annotations\AnnotationException : [Semantical Error] The annotation "..." in property Class::$property does not exist, or could not be auto-loaded.
  • Doctrine\ORM\Mapping\MappingException : Class "..." is not a valid entity or mapped super class.

Packages

composer require doctrine/orm
composer require symfony/validator
composer require zircote/swagger-php

Working configuration

// Composer autoloader class
$loader = require '/vendor/autoload.php'; // replace according the project

// tells AnnotationRegistry to use the Composer autoloader class
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']);

// Doctrine ORM configuration
$config = ORM\Tools\Setup::createAnnotationMetadataConfiguration(
    '/src/Resources', // replace according the project
    App::isDevEnv(), // replace according the project
    '/cache/doctrine/proxies', // replace according the project
    $cache, // replace according the project
    false // MUST BE SET TO 'false'
);

// ...
// later call to Swagger

$OA = \Swagger\scan(['/src/App.php', '/src/Resources']);

header('Content-Type: application/json');
echo $OA;

exit;
@SimonDevelop
Copy link

You saved my long day, thank you!

@cdomigan
Copy link

Thanks this solved my problem! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment