XXX is not a valid entity or mapped super class // and config options
Asked Answered
D

2

6

I just started to use Doctrine2 and I have an error when I want to persist an entity. Here is my error: 'Class "Myapp\app\entity\user" is not a valid entity or mapped super class.'

eAccelerator is not installed on my computer.

My app (I don't use Symfony) structure is like that:

  • MyApp
    • app
      • entity
    • core
      • external
        • Doctrine

And I initialize Doctrine with:

$classLoader = new ClassLoader('Doctrine', CORE_PATH . 'external');
$classLoader->register();

$cache = new \Doctrine\Common\Cache\ArrayCache();

$doctrineConfig = new \Doctrine\ORM\Configuration();

$doctrineConfig->setMetadataCacheImpl($cache);
$doctrineConfig->setQueryCacheImpl($cache);

$driverImpl = $doctrineConfig->newDefaultAnnotationDriver(array(APP_PATH . 'entity'));
$doctrineConfig->setMetadataDriverImpl($driverImpl);

$doctrineConfig->setAutoGenerateProxyClasses(false);

$connectionOptions = array(
    'driver'   => $database['driver'],
    'dbname'   => $database['dbname'],
    'user'     => $database['username'],
    'password' => $database['password']
);

$this->em = EntityManager::create($connectionOptions, $doctrineConfig);

What is wrong with these lines ?

Also as I autoload my classes with an autoloader, is it necessary to use the Doctrine class loader ? If yes, are these lines correct ?

$classLoader = new ClassLoader('myApp\app\entity', APP_PATH . 'entity');
$classLoader->register();

$classLoader = new ClassLoader('myApp\core\entity', CORE_PATH . 'entity');
$classLoader->register();

Edit: I forgot to add an entity class

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class user
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

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

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

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

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

//getters
//setters
Dissonant answered 20/12, 2012 at 10:8 Comment(0)
D
0

Ok, so my entity was correct and I don't need to call classLoader.

below is the correct initialization:

    $classLoader = new ClassLoader('Doctrine', CORE_PATH . 'external/');
    $classLoader->register();

    AnnotationRegistry::registerFile(CORE_PATH.'external/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');

    $cache = new ArrayCache();

    $annotationReader = new AnnotationReader();

    $cacheReader = new CachedReader(
        $annotationReader, 
        $cache          
    );

    $driverChain = new DriverChain();

    $annotationDriver = new AnnotationDriver(
        $annotationReader,
        array(CORE_PATH . 'model/')
    );

    $driverChain->addDriver($annotationDriver, 'gallib\core\model');

    $doctrineConfig = new Configuration();

    $doctrineConfig->setProxyDir(CORE_PATH . 'model/proxy');
    $doctrineConfig->setProxyNamespace('gallib\core\model\proxy');
    $doctrineConfig->setAutoGenerateProxyClasses(true);
    $doctrineConfig->setMetadataDriverImpl($driverChain);
    $doctrineConfig->setMetadataCacheImpl($cache);
    $doctrineConfig->setQueryCacheImpl($cache);

    $database = $this->config->database;

    $connectionOptions = array(
        'driver'   => $database['driver'],
        'dbname'   => $database['dbname'],
        'user'     => $database['username'],
        'password' => $database['password']
    );

    $this->em = EntityManager::create($connectionOptions, $doctrineConfig);
Dissonant answered 20/12, 2012 at 23:1 Comment(0)
S
9

in php > 8.0

you must change in config/packages/doctrine.yaml the mapping type to attribute


doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '13'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: attribute # from annotation to attribute
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
```
Steal answered 13/1, 2022 at 15:7 Comment(1)
Certainly, when you use PHP 8 attributes, then the type must change here. Thanks a lot for the hint!Tintinnabulum
D
0

Ok, so my entity was correct and I don't need to call classLoader.

below is the correct initialization:

    $classLoader = new ClassLoader('Doctrine', CORE_PATH . 'external/');
    $classLoader->register();

    AnnotationRegistry::registerFile(CORE_PATH.'external/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');

    $cache = new ArrayCache();

    $annotationReader = new AnnotationReader();

    $cacheReader = new CachedReader(
        $annotationReader, 
        $cache          
    );

    $driverChain = new DriverChain();

    $annotationDriver = new AnnotationDriver(
        $annotationReader,
        array(CORE_PATH . 'model/')
    );

    $driverChain->addDriver($annotationDriver, 'gallib\core\model');

    $doctrineConfig = new Configuration();

    $doctrineConfig->setProxyDir(CORE_PATH . 'model/proxy');
    $doctrineConfig->setProxyNamespace('gallib\core\model\proxy');
    $doctrineConfig->setAutoGenerateProxyClasses(true);
    $doctrineConfig->setMetadataDriverImpl($driverChain);
    $doctrineConfig->setMetadataCacheImpl($cache);
    $doctrineConfig->setQueryCacheImpl($cache);

    $database = $this->config->database;

    $connectionOptions = array(
        'driver'   => $database['driver'],
        'dbname'   => $database['dbname'],
        'user'     => $database['username'],
        'password' => $database['password']
    );

    $this->em = EntityManager::create($connectionOptions, $doctrineConfig);
Dissonant answered 20/12, 2012 at 23:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.