Doctrine:创建模式(未知实体名称空间别名'C'。)

时间:2011-02-26 19:30:49

标签: php zend-framework doctrine

我是ORM和Doctrine的新手,所以请耐心等待我。

我现在或多或少地遵循入门教程(只是使用不同的表来使其更有趣)。我创建了两个定义我的模式的YAML文件非常简单:

用户:

User:
  type: entity
  table: users
  id:
    id:
      type: integer
      generator: AUTO
  fields:
    firstName:
      type: string
    lastName:
      type: string
    birthdate:
      type: datetime
    email:
      type: string
    username:
      type: string
    password:
      type: string
  oneToMany:
    pages:
      targetEntity: Page
      mappedBy: user

页:

Page:
  type: entity
  table: pages
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
    content:
      type: text
  manyToOne:
    user:
      targetEntity: User
      inversedBy: pages

现在我尝试使用SchemaTool创建数据库模式。当我运行此代码时:

$em = Zend_Registry::get('entityManager');
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$classes = array(
    $em->getClassMetadata('Entities\User'),
    $em->getClassMetadata('Entities\Page')
);

我收到此错误:

#0 C:\inetpub\wwwroot\zend\library\Doctrine\ORM\Configuration.php(150):
Doctrine\ORM\ORMException::unknownEntityNamespace('C')
#1
C:\inetpub\wwwroot\zend\library\Doctrine\ORM\Mapping\ClassMetadataFactory.php(155):
Doctrine\ORM\Configuration->getEntityNamespace('C')
#2 C:\inetpub\wwwroot\zend\library\Doctrine\ORM\EntityManager.php(247):
Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor('C:\inetpub\wwwr...')
#3
C:\inetpub\wwwroot\zend\application\modules\default\controllers\DoctrineUtilController.php(16):
Doctrine\ORM\EntityManager->getClassMetadata('C:\inetpub\wwwr...')
#4 C:\inetpub\wwwroot\zend\library\Zend\Controller\Action.php(513):
DoctrineUtilController->generateModelsAction()
#5 C:\inetpub\wwwroot\zend\library\Zend\Controller\Dispatcher\Standard.php(295):
Zend_Controller_Action->dispatch('generateModelsA...')
#6 C:\inetpub\wwwroot\zend\library\Zend\Controller\Front.php(954):
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http),
Object(Zend_Controller_Response_Http))
#7 C:\inetpub\wwwroot\zend\application\Bootstrap.php(177):
Zend_Controller_Front->dispatch()
#8 C:\inetpub\wwwroot\zend\library\Zend\Application.php(366):
Bootstrap->run()
#9 C:\inetpub\wwwroot\zend\public\index.php(54): Zend_Application->run()
#10 {main}    
    $tool->createSchema($classes);

任何可能存在问题的想法?


以防万一的其他信息。这是我在Bootstrap.php文件中创建实体管理器的方法:

protected function _initDoctrine() {
    // (1)
    $config = new \Doctrine\ORM\Configuration();

    // Proxy Configuration (2)
    $config->setProxyDir(APPLICATION_PATH.'/proxies');
    $config->setProxyNamespace('Application\Proxies');
    $config->setAutoGenerateProxyClasses(('development' === APPLICATION_ENVIRONMENT));

    // Mapping Configuration (3)
    $driverImpl = new Doctrine\ORM\Mapping\Driver\XmlDriver(APPLICATION_PATH.'/configs/mappings/yml');
    $config->setMetadataDriverImpl($driverImpl);

    // Caching Configuration (4)
    if ('development' === APPLICATION_ENVIRONMENT) {
        $cache = new \Doctrine\Common\Cache\ArrayCache();
    } else {
        $cache = new \Doctrine\Common\Cache\ApcCache();
    }
    $config->setMetadataCacheImpl($cache);
    $config->setQueryCacheImpl($cache);

    // database configuration parameters (5)
    $conn = array(
        'driver' => $this->configuration->database->adapter,
        'user' => $this->configuration->database->username,
        'password' => $this->configuration->database->password,
        'dbname' => $this->configuration->database->dbname
    );

    // obtaining the entity manager (6)
    $evm = new Doctrine\Common\EventManager();
    $this->entityManager = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
    Zend_Registry::set('entityManager', $this->entityManager);
}

1 个答案:

答案 0 :(得分:1)

似乎路径定义存在问题。

Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor('C:\inetpub\wwwr...')

这不应该是绝对路径,因为Doctrine假定它是具有命名空间的类名,因此它将'C:\'作为命名空间。

因此配置或自动加载器存在问题。

Doctrine中有一个沙箱,其中包含一个基本的工作配置,您可以将其作为起点。