Adam Hopkinson
Adam Hopkinson

Reputation: 28795

Doctrine annotation "@Doctrine\ORM\Annotation\Entity" in class does not exist or cannot be autoloaded

I'm following the Doctrine Getting Started tutorial to the letter.

I've created the Product class (by copy/pasting from the tutorial, to ensure no typos), but when I run

vendor/bin/doctrine orm:schema-tool:create

I get [OK] No Metadata Classes to process. It seems this is because of the useSimpleAnnotationReader parameter of Setup::createAnnotationMetadataConfiguration defaulting to true.

So if I change this to false:

$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode, null, null, false);

Running the schema-tool:create command above now returns:

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

when it should execute and then dump out the SQL generated.

Searching on SO and elsewhere, it seems this problem is commonly caused by using forward- instead of backward-slashes in the annotations (which I haven't) or other typos in the Product entity (which I copy/pasted), or using Doctrine as part of a wider framework (Zend, Symfony).

My Product.php:

<?php
// src/Product.php

use Doctrine\ORM\Annotation as ORM;

/**
 * @ORM\Entity @ORM\Table(name="products")
 **/
class Product
{
    /** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue **/
    protected $id;

    /** @ORM\Column(type="string") **/
    protected $name;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

Upvotes: 3

Views: 1753

Answers (1)

Matteo
Matteo

Reputation: 39390

Try use the following statement:

use Doctrine\ORM\Mapping as ORM;

instead of:

use Doctrine\ORM\Annotation as ORM;

Upvotes: 15

Related Questions