Doctrine ORM和Silex带来很多乐趣 - Doctrine MappingException:类不存在

时间:2015-05-27 13:01:09

标签: doctrine-orm silex

我正在使用DileDevDoctrineORMServicProvider和Silex。我创建了一个简单的模型作为概念证明。

我能够从我的Yml文件构建模型(PHP类) - 但是当我尝试生成模式,并填充数据库等 - 它都崩溃了,我收到错误消息:

[Doctrine\Common\Persistence\Mapping\MappingException]  
  Class 'DistrictType' does not exist 

我必须承认,我正在努力使用PHP命名空间以及它们与Doctrine Mappings和文件位置的关系 - 我确信这个问题很可能与这种混乱有关。

我将简要介绍一下我的目录布局和我正在使用的配置文件 - 希望有人能够发现我出错的地方:

目录布局

|
|---src
|   app.php           // app variable cration, autoloading etc ..
|   registration.php  // service registration etc ..
|---Entity            // Generated models go here
|---Resources
|   |-- mappings      // YamL model definitions here ...
|
|---config
|   cli-config.php
|   config.yml
|
|---web               
    index.php         // imports app.php
    composer.json
    .htaccess

来自registration.php的片段

$app->register(new DoctrineOrmServiceProvider, array(
            "orm.proxies_dir" => __DIR__.'/../cache/doctrine/proxy',
                "orm.em.options" => array(
                        "mappings" => array(
                            // Using actual filesystem paths
                            array(
                                   "type" => "yml",
                                   "namespace" => "Entity",
                                   "path" => __DIR__."/Resources/mappings"
                                ),  
                         ),  
                     ),  
             )); 

// Setup entity manager (Possible Hack)
$paths = array($app['orm.em.options']['mappings'][0]['path']);
$config = Setup::createYAMLMetadataConfiguration($paths, $app['debug']);
$app['orm.em'] = EntityManager::create($app['db.options'], $config);

CLI-config.php中

<?php

// ...

$app = require_once __DIR__.'/../src/app.php';

$isDevMode = $app['debug'];

$paths = array($app['orm.em.options']['mappings'][0]['path']);
$config = Setup::createYAMLMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($app['db.options'], $config);

$commands = array(
            new \UseAllFive\Command\LoadDataFixturesDoctrineCommand(),
    );

return ConsoleRunner::createHelperSet($entityManager);

现在这里有模特:

District.dcm.yml

District:
    type: entity
    table: pac_district
    repositoryClass: DistrictRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:        
        name:
            type: string
            length: 64
            nullable: false

    manyToOne:
        region:
            targetEntity: Region
            joinColumn:
                name: region_id
                referencedColumnName: id
                onDelete: RESTRICT
                onUpdate: CASCADE

        district_type:
            targetEntity: DistrictType
            joinColumn:
                name: district_type_id
                referencedColumnName: id
                onDelete: RESTRICT
                onUpdate: CASCADE
    uniqueConstraints:
        uidx_district_name:
            columns: [name]

DistrictType.dcm.yml

DistrictType:
    type: entity
    table: pac_district_type
    repositoryClass: DistrictTypeRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:        
        name:
            type: string
            length: 64
            nullable: false

    uniqueConstraints:
        uidx_district_type_name:
            columns: [name]

Region.dcm.yml

Region:                             
    type: entity
    table: pac_region       
    repositoryClass: RegionRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:        
        name:
            type: string
            length: 64
            nullable: false

    uniqueConstraints:
        uidx_region_name:
            columns: [name]

composer.json

{
    "require": { 
        "silex/silex": "2.0.*@dev",
        "symfony/yaml": "2.6.7",
        "doctrine/dbal": "~2.2",
        "doctrine/orm": ">=2.1",
        "dflydev/doctrine-orm-service-provider": "2.0.*@dev"
    },  
    "autoload": {
        "psr-0": {"Controller": "src/"}, 
        "psr-0": {"Entity": "src/"} 
    }   
}

这些是以下命令的结果:

root@yourbox:~/path/to/project$ vendor/bin/doctrine orm:generate-entities src/Entity/
Processing entity "Region"
Processing entity "DistrictType"
Processing entity "District"

Entity classes generated to "/path/to/project/src/Entity"


root@yourbox:~/path/to/project$ vendor/bin/doctrine orm:schema-tool:create

  [Doctrine\Common\Persistence\Mapping\MappingException]  
  Class 'DistrictType' does not exist        

我已经连续两天挣扎了 - 而且看不出我做错了什么 - 有人能发现Imay做错了吗?

2 个答案:

答案 0 :(得分:0)

实际上很简单。你做错了是 - 自动加载文件。现在配置您的composer文件的方式,它不会自动加载实体文件,并且架构创建将失败。

通过将空字符串作为第一个参数传递来修改您的composer autoload部分,并将Entity添加到路径中,如下所示:

"autoload": {
    "psr-0": {"Controller": "src/"}, 
    "psr-0": {"": "src/Entity/"} 
}

然后运行composer update,您就可以生成实体了。另外,目前我正在与Silex一起开发一个小项目,我强烈建议您切换到使用命名空间。

答案 1 :(得分:0)

你好@Artamiel修复了我的问题,它是在编辑器中添加自动加载和修复命名空间的组合,我的意思是我的自动加载现在有:

"psr-0": {"Entity": "config/doctrine/"}

因为我已将命名空间定义为Entity创建了这样一个文件夹并将实体移动到那里,最后我就开始运行了。感谢