I want to use Doctrine 2 in my project. I've some problems with it. I read the docs but probably I'm doing something wrong.
I want to autoload entities classes. And method from the docs is not working.
My bootstrap.php
<?php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver;
$paths = array("../Entities");
$isDevMode = false;
$classLoader = new \Doctrine\Common\ClassLoader('Entities','../Entities');
$classLoader->register();
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'xxx',
'password' => 'xxx',
'dbname' => 'xxx',
);
$driver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(new Doctrine\Common\Annotations\AnnotationReader(),array('../Entities'));
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$config->setMetadataDriverImpl($driver);
$em = EntityManager::create($dbParams, $config);
I've got my model's classes in Entities directory. I've generated them using doctrine client. They looks OK. There are ids, setters, getters and namespace at the beginning.
/Entities/ArticleCat.php
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* ArticleCat
*
* @ORM\Table(name="article_cat")
* @ORM\Entity
*/
class ArticleCat
{
My script where I want to use doctrine:
<?php
require_once 'bootstrap.php';
$article = $em->find('ArticleCat', 21);
echo $article->getName();
It's not working. It works only when I use it this way and I remove namespace from Entity model.
<?php
require_once 'bootstrap.php';
require_once 'Entities/ArticleCat.php'; //this line added (manual load)
$article = $em->find('ArticleCat', 21);
echo $article->getName();
What's the proper way of using doctrine and autoload entities? Why the namespace is a problem?
My errors:
Fatal error: Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException'
with message 'Class 'ArticleCat' does not exist' in
/myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96
Stack trace: #0 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php(40):
Doctrine\Common\Persistence\Mapping\MappingException::nonExistingClass('ArticleCat')
#1 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping
/AbstractClassMetadataFactory.php(267): Doctrine\Common\Persistence\Mapping
\RuntimeReflectionService->getParentClasses('ArticleCat')
#2 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping
/AbstractClassMetadataFactory.php(297): Doctrine\Common\Persistence\Mapping
\AbstractClassMetadataFactory->getParentClasses('ArticleCat')
#3 /myproject/vendor/doc in /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence
/Mapping/MappingException.php on line 96