Doctrine 2无法找到实体

时间:2010-12-26 14:49:09

标签: doctrine kohana kohana-3 doctrine-orm

我正在使用Kohana 3并且我的实体内部有/ doctrine / Entites文件夹。执行代码时

$product = Doctrine::em()->find('Entities\Product', 1);

在我的控制器中,我收到了错误

class_parents(): Class Entities\Product does not exist and could not be loaded

下面是Controller(classes / controller / welcome.php):

<?php

class Controller_Welcome extends Controller {

    public function action_index()
    {
        $prod = Doctrine::em()->find('Entities\Product', 1);
    }

}

以下是实体(/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; }
}

以下是Doctrine模块引导程序文件(/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\ClassLoader('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);
    }
}

任何想法我做错了什么?

3 个答案:

答案 0 :(得分:3)

更新:名称空间实体;必须在每个实体的开头

这与自动加载器有关。我对Doctrine 2很新(甚至是1.2的新手),但我认为它在你的:

    $classLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'doctrine');
    $classLoader->register();

尝试添加realpath(APPPATH.'doctrine')。我使用Zend Framework,因此它在引导程序中看起来有点不同,但也许它会有所帮助:

/**
 * 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;
}

答案 1 :(得分:1)

原来我必须添加

namespace Entities;

到实体文件的顶部。这不是在任何教程中编写的。感谢用户Max帮助IRC。

答案 2 :(得分:0)

只是为了详细说明Max Gordon的答案,这对我有用

如果您在顶部

中有“名称空间实体”内的/ models / entites
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(APPLICATION_PATH . '/models/'), 'loadClass');
$classLoader->register();

或使用zend

$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(APPLICATION_PATH . '/models/'), 'loadClass');
$autoloader = Zend_Loader_Autoloader::getInstance();     
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');