DDC-1644: Using full annotation namespace breaks annotations #2065

Closed
opened 2026-01-22 13:39:20 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 10, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user jreed:

I have created my own custom annotations. They work fine when I use them in a normal class file, but if I try to use them inside of a Doctrine Model class then the base Doctrine annotations don't work. My understanding is I have to specify the full namespace for the default Annotations in order for them to work with custom annotations. However, when I do this I get the dreaded error:

Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class xxx is not a valid entity or mapped super class.' in /Magnet/winlibrary/doctrine-orm/Doctrine/ORM/Mapping/MappingException.php:142

In order to test this further I used the quick start documentation and modified it to use full annotation namespaces. Here is the code:

<?php
require_once '/library/doctrine-orm/Doctrine/Common/ClassLoader.php';

$loader = new \Doctrine\Common\ClassLoader("Doctrine", '/library/doctrine-orm');
$loader->register();

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping as ORM;

/*** @ORM\Entity ***/
class Post
{
    /*** @ORM\Id @GeneratedValue @ORM\Column(type="integer") ***/
    protected $id;
    /*** @ORM\Column(type="string") ***/
    protected $title;
    /*** @ORM\Column(type="text") ***/
    protected $body;
}

$dbParams = array(
    'driver' => 'pdo_mysql',
    'user' => 'root',
    'password' => '',
    'dbname' => 'mydb'
);

$config = Setup::createAnnotationMetadataConfiguration(array());
$entityManager = EntityManager::create($dbParams, $config);
$post = $entityManager->find("Post", 555);

print_r($post);

When I run this I get the error:

Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class Post is not a valid entity or mapped super class.' in /Magnet/winlibrary/doctrine-orm/Doctrine/ORM/Mapping/MappingException.php:142

If I remove the "ORM" from the annotations and remove "use Doctrine\ORM\Mapping as ORM;" then it works properly. I can't seem to find why the full annotation namespaces don't work properly as I've seen a number of examples that do this.

Also, I noticed that if I specify the full namespace without the "use Doctrine\ORM\Mapping as ORM;" alias (i.e. @Doctrine\ORM\Mapping\Entity) then it works on 2.1.x but does not work on 2.2.0. Using the ORM alias does not work on either version. Given I've followed the documentation and have found inconsistencies between the versions it seems this is a bug in either the code or the documentation.

Originally created by @doctrinebot on GitHub (Feb 10, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user jreed: I have created my own custom annotations. They work fine when I use them in a normal class file, but if I try to use them inside of a Doctrine Model class then the base Doctrine annotations don't work. My understanding is I have to specify the full namespace for the default Annotations in order for them to work with custom annotations. However, when I do this I get the dreaded error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class xxx is not a valid entity or mapped super class.' in /Magnet/winlibrary/doctrine-orm/Doctrine/ORM/Mapping/MappingException.php:142 In order to test this further I used the quick start documentation and modified it to use full annotation namespaces. Here is the code: ``` <?php require_once '/library/doctrine-orm/Doctrine/Common/ClassLoader.php'; $loader = new \Doctrine\Common\ClassLoader("Doctrine", '/library/doctrine-orm'); $loader->register(); use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping as ORM; /*** @ORM\Entity ***/ class Post { /*** @ORM\Id @GeneratedValue @ORM\Column(type="integer") ***/ protected $id; /*** @ORM\Column(type="string") ***/ protected $title; /*** @ORM\Column(type="text") ***/ protected $body; } $dbParams = array( 'driver' => 'pdo_mysql', 'user' => 'root', 'password' => '', 'dbname' => 'mydb' ); $config = Setup::createAnnotationMetadataConfiguration(array()); $entityManager = EntityManager::create($dbParams, $config); $post = $entityManager->find("Post", 555); print_r($post); ``` When I run this I get the error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class Post is not a valid entity or mapped super class.' in /Magnet/winlibrary/doctrine-orm/Doctrine/ORM/Mapping/MappingException.php:142 If I remove the "ORM\" from the annotations and remove "use Doctrine\ORM\Mapping as ORM;" then it works properly. I can't seem to find why the full annotation namespaces don't work properly as I've seen a number of examples that do this. Also, I noticed that if I specify the full namespace without the "use Doctrine\ORM\Mapping as ORM;" alias (i.e. @Doctrine\ORM\Mapping\Entity) then it works on 2.1.x but does not work on 2.2.0. Using the ORM alias does not work on either version. Given I've followed the documentation and have found inconsistencies between the versions it seems this is a bug in either the code or the documentation.
admin added the Bug label 2026-01-22 13:39:20 +01:00
admin closed this issue 2026-01-22 13:39:22 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 17, 2012):

Comment created by @beberlei:

Setup::createAnnotationMetadataDriver().. uses the Simple Annotation Reader.

You have to setup the "normal" annotation reader with the AnnotationDriver to get "ORM" working. This could be documented better.

@doctrinebot commented on GitHub (Feb 17, 2012): Comment created by @beberlei: Setup::createAnnotationMetadataDriver().. uses the Simple Annotation Reader. You have to setup the "normal" annotation reader with the AnnotationDriver to get "ORM\" working. This could be documented better.
Author
Owner

@doctrinebot commented on GitHub (Feb 17, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Feb 17, 2012): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2065