Doctrine orm Doctrine ORM和Silex非常有趣-Doctrine MappingException:类不存在
我正在将DFLYDEVDOCTRINEORMServiceProvider与Silex一起使用。我创建了一个简单的模型作为概念证明 我能够从我的Yml文件中构建模型PHP类-但是当我尝试生成模式、填充数据库等时,所有这些都崩溃了,我得到了错误消息:Doctrine orm Doctrine ORM和Silex非常有趣-Doctrine MappingException:类不存在,doctrine-orm,silex,Doctrine Orm,Silex,我正在将DFLYDEVDOCTRINEORMServiceProvider与Silex一起使用。我创建了一个简单的模型作为概念证明 我能够从我的Yml文件中构建模型PHP类-但是当我尝试生成模式、填充数据库等时,所有这些都崩溃了,我得到了错误消息: [Doctrine\Common\Persistence\Mapping\MappingException] Class 'DistrictType' does not exist 我必须承认,我正在与PHP名称空间以及它们与条令映射和文
[Doctrine\Common\Persistence\Mapping\MappingException]
Class 'DistrictType' does not exist
我必须承认,我正在与PHP名称空间以及它们与条令映射和文件位置的关系作斗争——我相信问题可能与这种混淆有关
我将简要介绍我的目录布局和使用的配置文件-希望有人能够发现我的错误:
目录布局
registration.php中的代码片段
cli-config.php
DistrictType.dcm.yml
Region.dcm.yml
composer.json
以下是以下命令的结果:
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
我已经连续两天为这个问题苦苦挣扎了——看不出我做错了什么——有人能看出我做错了什么吗?事实上这很简单。你做错的是-自动加载文件。按照现在配置composer文件的方式,它将不会自动加载实体文件,并且模式创建将失败 通过传递空字符串作为第一个参数来修改composer autoload部分,并将实体添加到路径中,如下所示:
"autoload": {
"psr-0": {"Controller": "src/"},
"psr-0": {"": "src/Entity/"}
}
然后运行composer update,您将能够生成实体。另一方面,目前我也在使用Silex开发一个小项目,我强烈建议您改用名称空间。Hello@Artamiel解决了我的问题,它是在composer中添加自动加载和修复名称空间的组合,我的自动加载现在有:
"psr-0": {"Entity": "config/doctrine/"}
我已经将名称空间定义为实体,创建了这样一个文件夹并将实体移动到那里,最后我开始运行了。谢谢您能给我们看一下生成的实体代码吗?您不应该用其他答案回答,而应该用注释。另外,如果@Artamiel帮忙,表现得很好,我不能对他的回答发表评论或接受,我很愿意,但我可以。他的回答对我帮助很大,所以我想感谢他我的错,我以为你是OP,我看你不是,对不起!另外,你的代表太少,无法发表评论,下次我会保持沉默,我保证:-
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:
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:
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]
{
"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
"autoload": {
"psr-0": {"Controller": "src/"},
"psr-0": {"": "src/Entity/"}
}
"psr-0": {"Entity": "config/doctrine/"}