首页 > 解决方案 > 如何为单个实体使用 XML 映射并保持由注释映射的其余实体?

问题描述

我正在向基于 Symfony 2.8 的旧应用程序添加新功能(hackzilla/Ticket-bundle),我需要扩展这个包的基本实体以便能够添加一些自定义字段。

在应用程序中,所有实体都使用注释进行映射,但要从 Ticket-bundle 扩展实体,我很可能需要使用 XML 映射,基于此doc

有没有办法为单个/多个实体使用 XML 映射,但不是全部?

这是我目前的学说配置。

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%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        dql:
           datetime_functions:
               month: DoctrineExtensions\Query\Mysql\Month
               monthname: DoctrineExtensions\Query\Mysql\MonthName
               ifnull: DoctrineExtensions\Query\Mysql\IfNull

标签: symfonysymfony-2.8

解决方案


是的,您需要禁用doctrine.orm.auto_mapping并改为手动映射实体。它可能看起来像 thisc(您可以省略其中一些选项,在使用捆绑包时查看链接文档以获取更短的示例):

doctrine:
    orm:
        mappings:
            App:
                is_bundle: true
                type: annotation
                dir: '%kernel.project_dir%/src/AppBundle/Entity'
                prefix: 'AppBundle\Entity'
                alias: App
            Ticket:
                is_bundle: true
                type: xml
                dir: '%kernel.project_dir%/src/TicketBundle/Entity'
                prefix: 'TicketBundle\Entity'
                alias: Ticket

另见:https ://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration


推荐阅读