Я использую DflyDevDoctrineORMServicProvider с Silex. Я создал простую модель в качестве доказательства концепции.
Я могу построить модель (классы PHP) из файлов Yml, но когда я пытаюсь сгенерировать схему и заполнить базу данных и т. д., все разваливается, и я получаю сообщение об ошибке:
[Doctrine\Common\Persistence\Mapping\MappingException]
Class 'DistrictType' does not exist
Должен признаться, я борюсь с пространствами имен PHP и тем, как они связаны с сопоставлениями Doctrine и расположением файлов — я уверен, что проблема, вероятно, связана с этой путаницей.
Я кратко опишу структуру моего каталога и файлы конфигурации, которые я использую — надеюсь, кто-то сможет заметить, где я ошибаюсь:
Макет каталога
|
|---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);
Теперь вот модели:
Район.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]
Регион.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
Я боролся с этим два дня подряд - и не вижу, что я делаю неправильно - может ли кто-нибудь определить, что я делаю неправильно?