Doctrine error: Class 'Doctrine\ORM\EntityRepo

2019-03-01 14:22发布

I'm new in Doctrine, I'm following the examples in the documentation. When I create a Repository class (e.g. UserRepository.php) and I try to extend EntityRepository class I get this error message: Fatal error: Class 'Doctrine\ORM\EntityRepository' not found in C:\xampp\htdocs\excap\repositories\UserRepository.php on line 10

This is my Repository class:

use Doctrine\ORM\EntityRepository;

/**
 * Description of UserRepository
 *
 * @author Mario
 */
class UserRepository extends EntityRepository
{ 

What am I doing wrong? (by the way, I installed Doctrine2 using Composer)

<?php
//bootstrap_doctrine.php
$isDevMode = true;
use Doctrine\ORM\Tools\Setup;
require_once 'vendor/autoload.php';

$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__.'/entities'), $isDevMode);
$conn = array(
    'driver' => 'pdo_mysql',
    'user' => 'mydbuser',
    'password' => 'mydbpassword',
    'dbname' => 'mydbname'
);

$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
?>

<?php
//boostrap.php
//Entities
require_once 'entities/User.php';
require_once 'entities/Gender.php';
require_once 'entities/AccessPoint.php';

//Repositories
require_once 'repositories/UserRepository.php';

if(!class_exists("Doctrine\Common\Version", FALSE))
{
    require_once 'bootstrap_doctrine.php';
}
?>

<?php
// autoload.php generated by Composer
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
    require __DIR__ . '/composer' . '/ClassLoader.php';
}

return call_user_func(function() {
    $loader = new \Composer\Autoload\ClassLoader();
    $composerDir = __DIR__ . '/composer';

    $map = require $composerDir . '/autoload_namespaces.php';
    foreach ($map as $namespace => $path) {
        $loader->add($namespace, $path);
    }

    $classMap = require $composerDir . '/autoload_classmap.php';
    if ($classMap) {
        $loader->addClassMap($classMap);
    }

    $loader->register();

    return $loader;
});
?>

2条回答
Luminary・发光体
2楼-- · 2019-03-01 15:04

Problem solved. The problem was in the bootstrap.php file. I was loading the entities before loading the bootstrap_doctrine.php; that's all.

查看更多
时光不老,我们不散
3楼-- · 2019-03-01 15:05

Did you put namespace in your repository class?

查看更多
登录 后发表回答