I just started to use Doctrine2 and I have an error when I want to persist an entity. Here is my error: ‘Class “Myappappentityuser” 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
- external
- app
And I initialize Doctrine with:
$classLoader = new ClassLoader('Doctrine', CORE_PATH . 'external'); $classLoader->register(); $cache = new DoctrineCommonCacheArrayCache(); $doctrineConfig = new DoctrineORMConfiguration(); $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('myAppappentity', APP_PATH . 'entity'); $classLoader->register(); $classLoader = new ClassLoader('myAppcoreentity', CORE_PATH . 'entity'); $classLoader->register();
Edit: I forgot to add an entity class
use DoctrineORMMapping as ORM;
/** * @ORMEntity */ class user { /** * @ORMColumn(name="id", type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */ protected $id; /** * @ORMColumn(name="username", type="string", length=255) */ protected $username; /** * @ORMColumn(name="nicename", type="string", length=255) */ protected $nicename; /** * @ORMColumn(name="email", type="string", length=255) */ protected $email; /** * @ORMColumn(name="password", type="string", length=255) */ protected $password; //getters //setters
Advertisement
Answer
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, 'gallibcoremodel'); $doctrineConfig = new Configuration(); $doctrineConfig->setProxyDir(CORE_PATH . 'model/proxy'); $doctrineConfig->setProxyNamespace('gallibcoremodelproxy'); $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);