andybeak
2/12/2015 - 1:03 PM

This fixes the "“class” is not a valid entity or mapped super class". See https://stackoverflow.com/questions/15099060/doctrine2-class-is-n

This fixes the "“class” is not a valid entity or mapped super class". See https://stackoverflow.com/questions/15099060/doctrine2-class-is-not-a-valid-entity-or-mapped-super-class

<?php

/**
 * Configures and provides an Entity Manager
 */

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;


class EntityProvider
{
    public static $em;

    private function __construct()
    {}

    public static function getInstance()
    {
        if(!is_object(self::$em))
        {
            $entityPaths = [DE_PATH . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'entities' . DIRECTORY_SEPARATOR];

            $dbParams = array(
                'driver'   => 'pdo_mysql',
                'user'     => DB_USER,
                'password' => DB_PASSWORD,
                'dbname'   => DB_NAME,
                'host'     => DB_HOST
            );

            $config = Setup::createAnnotationMetadataConfiguration($entityPaths, true);

            $driver = new AnnotationDriver(new AnnotationReader(), $entityPaths);

            // registering noop annotation autoloader - allow all annotations by default
            AnnotationRegistry::registerLoader('class_exists');

            $config->setMetadataDriverImpl($driver);

            self::$em = EntityManager::create($dbParams, $config);

        }

        return self::$em;
    }

}