【问题标题】:What is the standard way to use a bundle's entities in symfony?在 symfony 中使用捆绑实体的标准方法是什么?
【发布时间】:2020-05-05 18:30:50
【问题描述】:

我一直在尝试学习如何在 symfony 中使用包,但遇到了一些问题,使包的实体出现在我的数据库中。我终于通过在我的学说.yaml 文件中添加最后几行来让它工作。但是由于某种原因,这似乎不太可能是正确的方法,因为它仅在我将“is_bundle”属性设置为 false 时才有效,而它可能应该为 true,因为它是来自捆绑。

让这个实体出现在我的数据库中的正确方法是什么?

我的教义文件:

doctrine:
  dbal:
    default_connection: default
    connections:
      default:
        # configure these for your database server
        url: '%env(DATABASE_URL)%'
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        unix_socket: '/Applications/MAMP/tmp/mysql/mysql.sock'
        default_table_options:
          charset: utf8mb4
          collate: utf8mb4_unicode_ci

  orm:
    default_entity_manager: default
    entity_managers:
      default:
        connection: default
        mappings:
          default:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Entity'
            prefix: 'App\Entity'
            alias: default
          ch_cookie_consent_bundle:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/vendor/connectholland/cookie-consent-bundle/Entity'
            prefix: 'ConnectHolland\CookieConsentBundle\Entity'
            alias: ch_cookie_consent_bundle 

捆绑包本身可以在这里找到:https://github.com/ConnectHolland/cookie-consent-bundle

【问题讨论】:

  • 我知道这是违反直觉的,但 is_bundle 应该始终为假。如果设置为 true,则可以使用一些特定于捆绑包的快捷方式,但不再有人使用它们。您曾经看到诸如 MyBundle:Entity 之类的东西,但现在每个人都只使用完整的类名。您的配置看起来正确。
  • 您是否尝试创建迁移脚本? symfony console make:migrationphp bin/console doctrine:migrations:diff

标签: php symfony doctrine


【解决方案1】:

is_bundle 标志指示原则扩展将配置键与包名称匹配。

在你的情况下,配置应该是

  orm:
    mappings:
      default:
        ...
      CHCookieConsentBundle: # This should match the bundle name (in %kernel.bundles%)
        is_bundle: true
        alias: ch_cookie_consent_bundle

所有其他配置键都是可选的并且可以自动检测。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    相关资源
    最近更新 更多