mgdepoo mgdepoo - 1 month ago 10
PHP Question

Symfony and Doctrine: mapping entities that live outside of a bundle

I have a symfony project with some DDD flavor to it, and I'm trying to map my domain entities with doctrine and yml.

My (relevant) folder structure is like this:

src/
|_ Vendor/
|_ Project/
|_ Application/
|_ Domain/
| |_ Model/
| |_ User/
| |_ User.php # entity
| |_ UserId.php # value object
| |_ Username.php # value object
|_ Infrastructure/
|_ AppBundle/
|_ Resources/
|_ config/
|_ doctrine/
|_ user.yml


My user.yml file is like this:

User\User:
type: entity
table: user
id:
UserId:
type: User\UserId
column: id
fields:
username:
type: User\Username
column: username


And in my config.yml file I have this:

# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8

orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
mappings:
domain:
type: yml
dir: "%kernel.root_dir%/../src/Vendor/Project/Infrastructure/AppBundle/Resources/config/doctrine"
is_bundle: false
prefix: Vendor\Project\Domain\Model
alias: Domain


No matter what I try, any doctrine operation such as Symfony's
php bin/console doctrine:schema:create --dump-sql
returns
No Metadata Classes to process.
.

I don't know if I'm missing something here, it is the first time a try such an approach.

Any tips? Thanks in advance.

Answer

Ok, after an exhausting process of trial and error, the working configuration was like this:

src/
|_ Vendor/
    |_ Project/
       |_ Application/
       |_ Domain/
       |  |_ Model/
       |     |_ User/
       |        |_ User.php # entity
       |        |_ UserId.php # value object
       |        |_ Username.php # value object
       |_ Infrastructure/
          |_ AppBundle/
             |_ Resources/
                |_ config/
                   |_ doctrine/
                      |_ User.User.orm.yml # namespace AFTER prefix from config.yml

User.User.orm.yml: # full namespaces

Vendor\Project\Domain\Model\User\User:
    type: entity
    table: user
    id:
        id: # name is not anymore the name of the VO
            type: Vendor\Project\Domain\Model\User\UserId
            column: id
    fields:
        username:
            type: Vendor\Project\Domain\Model\User\Username
            column: username

config.yml:

orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    default_entity_manager: default
    entity_managers:
        default:
            auto_mapping: false
            mappings:
                domain:
                    type: yml
                    dir: "%kernel.root_dir%/../src/Vendor/Project/Infrastructure/AppBundle/Resources/config/doctrine"
                    is_bundle: false
                    prefix: "Vendor\\Project\\Domain\\Model"
                    alias: Model

Basically, the mapping file name must follow the convention of .orm.yml extension, plus must implement the final part of the FQCN (the part not covered by the prefix: in the config.yml.

Also, in the mapping file, types must be FQCNs.

Hope this helps anyone with the same problem.

EDIT: thanks nifr for the help

Comments