0

我正在使用 Kohana 3 并有一个 /doctrine/Entites 文件夹,里面有我的实体。执行代码时

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

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

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

下面是控制器(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);
    }
}

任何想法我做错了什么?

4

3 回答 3

3

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

这与自动装载机有关。我对 Doctrine 2 非常陌生(甚至是 1.2 的新手),但我认为它在你的:

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

尝试添加一个真实路径(APPPATH.'doctrine')。我使用 Zend 框架,所以它在引导程序中看起来有点不同,但也许它会有所帮助:

/**
 * 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;
}
于 2010-12-26T15:32:51.823 回答
1

原来我不得不添加

namespace Entities;

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

于 2010-12-26T15:44:38.593 回答
0

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

如果您的实体在 /models/ 中,顶部有“命名空间实体”

$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');
于 2011-11-21T15:28:38.223 回答