I'm using Kohana 3 and have a /doctrine/Entites folder with my entities inside. When executing the code
$product = Doctrine::em()->find('Entities\Product', 1);
in my controller, I get the error
class_parents(): Class Entities\Product does not exist and could not be loaded
Below is the Controller (classes/controller/welcome.php):
<?php
class Controller_Welcome extends Controller {
public function action_index()
{
$prod = Doctrine::em()->find('Entities\Product', 1);
}
}
Below is the Entity (/doctrine/Entities/Product.php):
<?php
/**
* @Entity
* @Table{name="products"}
*/
class Product
{
/** @Id @Column{type="integer"} */
private $id;
/** @Column(type="string", length="255") */
private $name;
public function getId() { return $this->id; }
public function setId($id) { $this->id = intval($id); }
public function getName() { return $this->name; }
public function setName($name) { $this->name = $name; }
}
Below is the Doctrine module bootstrap file (/modules/doctrine/init.php):
class Doctrine
{
private static $_instance = null;
private $_application_mode = 'development';
private $_em = null;
public static function em()
{
if ( self::$_instance === null )
self::$_instance = new Doctrine();
return self::$_instance->_em;
}
public function __construct()
{
require __DIR__.'/classes/doctrine/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', __DIR__.'/classes/doctrine');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', __DIR__.'/classes/doctrine/Doctrine');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoad开发者_高级运维er('Entities', APPPATH.'doctrine');
$classLoader->register();
//Set up caching method
$cache = $this->_application_mode == 'development'
? new \Doctrine\Common\Cache\ArrayCache
: new \Doctrine\Common\Cache\ApcCache;
$config = new Configuration;
$config->setMetadataCacheImpl( $cache );
$driver = $config->newDefaultAnnotationDriver( APPPATH.'doctrine/Entities' );
$config->setMetadataDriverImpl( $driver );
$config->setQueryCacheImpl( $cache );
$config->setProxyDir( APPPATH.'doctrine/Proxies' );
$config->setProxyNamespace('Proxies');
$config->setAutoGenerateProxyClasses( $this->_application_mode == 'development' );
$dbconf = Kohana::config('database');
$dbconf = reset($dbconf); //Use the first database specified in the config
$this->_em = EntityManager::create(array(
'dbname' => $dbconf['connection']['database'],
'user' => $dbconf['connection']['username'],
'password' => $dbconf['connection']['password'],
'host' => $dbconf['connection']['hostname'],
'driver' => 'pdo_mysql',
), $config);
}
}
Any ideas what I've done wrong?
UPDATE: namespace Entities; has to be in the beginning of every Entity
It's something with the autoloader. I'm very new to Doctrine 2 (even new to 1.2) but I think it's in your:
$classLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'doctrine');
$classLoader->register();
Try adding a realpath(APPPATH.'doctrine'). I use Zend Framework so it looks a little different in the bootstrap but maybe it'll help:
/**
* Initialize auto loader of Doctrine
*
* @return Doctrine_Manager
*/
protected function _initDoctrine() {
$this->bootstrap('autoload');
require_once('Doctrine/Common/ClassLoader.php');
/*
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine');
$classLoader->setIncludePath(APPLICATION_PATH . '/../library/');
$classLoader->register();/* */
// Create the doctrine autoloader and remove it from the spl autoload stack (it adds itself)
require_once 'Doctrine/Common/ClassLoader.php';
$doctrineAutoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');
//$doctrineAutoloader->register();
spl_autoload_unregister($doctrineAutoloader);
$autoloader = Zend_Loader_Autoloader::getInstance();
// Push the doctrine autoloader to load for the Doctrine\ namespace
$autoloader->pushAutoloader($doctrineAutoloader, 'Doctrine');
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(__DIR__ . '/models/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', realpath(__DIR__ . '/../library/Doctrine/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Symfony');
$doctrineConfig = $this->getOption('doctrine');
$config = new \Doctrine\ORM\Configuration();
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$driverImpl = new Doctrine\ORM\Mapping\Driver\YamlDriver(APPLICATION_PATH . '/configs/mappings/yaml');
//$driverImpl = $config->newDefaultAnnotationDriver($doctrineConfig['path']['entities']);
$config->setMetadataDriverImpl($driverImpl);
//$driverImpl = $config->newDefaultAnnotationDriver(
// array($doctrineConfig['paths']['entities']));
//$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(APPLICATION_PATH . '/../proxies');
$config->setProxyNamespace('App\Proxies');
$connectionOptions = array(
'driver' => $doctrineConfig['conn']['driv'],
'user' => $doctrineConfig['conn']['user'],
'password' => $doctrineConfig['conn']['pass'],
'dbname' => $doctrineConfig['conn']['dbname'],
'host' => $doctrineConfig['conn']['host']
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$eventManager = $em->getEventManager();
$eventManager->addEventSubscriber(new Maxlib_EventSubscriber_Sortable());
$registry = Zend_Registry::getInstance();
$registry->entitymanager = $em;
return $em;
}
Turns out I had to add
namespace Entities;
to the top of the entity file. This was not written in any of the tutorials. Thanks to user Max for helping on IRC.
Just to elaborate Max Gordon's answer, this is what worked for me
If you have your entites inside /models/ with "namespace Entities" at the top
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(APPLICATION_PATH . '/models/'), 'loadClass');
$classLoader->register();
or using zend
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(APPLICATION_PATH . '/models/'), 'loadClass');
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');
精彩评论