开发者

Doctrine: Creating schema (Unknown Entity namespace alias 'C'.)

开发者 https://www.devze.com 2023-02-13 11:20 出处:网络
I am very new to ORM and Doctrine so please be patient with me. I am more or less following the getting started tutorial now (just with different tables to make it more interesting). I have created t

I am very new to ORM and Doctrine so please be patient with me.

I am more or less following the getting started tutorial now (just with different tables to make it more interesting). I have created two YAML files defining my schema which is very simple:

User:

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:

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

Now I am trying to use the SchemaTool to create a database schema. When I run this code:

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

I get this error:

#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);

Any ideas where could be a problem?


Additional information just in case. This is how I create entity manager in the Bootstrap.php file:

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->co开发者_JAVA技巧nfiguration->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);
}


It seems that there is a problem with the path definitions.

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

This shouldn't be the absolute path, as Doctrine assumes that is the class name with namespace, so it takes that 'C:\' as namespace.

So it is a problem with configuration or autoloaders.

There is a sandbox in the Doctrine which includes a basic working configuration, you can use that as a starting point.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号