1
votes

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
1

1 Answers

0
votes

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);