【问题标题】:Can't add a field to a model in Symfony, bin/console crashes无法在 Symfony 中向模型添加字段,bin/console 崩溃
【发布时间】:2019-01-31 15:26:33
【问题描述】:

我正在使用 Sylius 框架。我正在关注guide to customize models

我正在尝试将字段 notice 添加到模型 Taxon 中,该模型已在我的项目中被覆盖。为此,我在模型的Taxon.orm.yml 中添加了字段描述:

MyProject\Bundle\ShopBundle\Entity\Taxon:
    type: entity
    table: sylius_taxon

    # {Relationships code...}

    fields:
        # {Some existing fields...}

        notice:
            type: text
            nullable: true

我还在覆盖的Taxon 类中添加了一个字段、一个getter 和一个setter。

然后我尝试运行bin/console doctrine:migrations:diff,但是当我运行bin/console 时,即使没有任何参数,它也会崩溃并出现以下异常:

[Doctrine\DBAL\Exception\InvalidFieldNameException]
  An exception occurred while executing 'SELECT s0_.code AS code_0, s0_.tree_left AS tree_left_1, s0_.tree_right AS tree_right_2, s0_.tree_level AS tree_level_3, s0_.position AS position_4, s0_.id AS id_5, s0_
  .created_at AS created_at_6, s0_.updated_at AS updated_at_7, s0_.enabled AS enabled_8, s0_.default_markup AS default_markup_9, s0_.notice AS notice_10, s0_.tree_root AS tree_root_11, s0_.parent_id AS parent_
  id_12 FROM sylius_taxon s0_ WHERE s0_.parent_id IS NULL ORDER BY s0_.tree_left ASC':
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 's0_.notice' in 'field list'`


[Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 's0_.notice' in 'field list'`


[PDOException]
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 's0_.notice' in 'field list'

如果我删除对Taxon.orm.yml 的更改,那么bin/console 将再次起作用。我的更改中缺少什么?

【问题讨论】:

  • 看起来这个模型的存储库被注入到我的一个自定义控制台命令中,这会导致问题。当我找到那个命令时,我会发布答案。

标签: symfony doctrine-orm sylius symfony-3.2


【解决方案1】:

我的一个捆绑包的配置包含该模型的存储库,就是这样。我暂时从config.yml删除了bundle的配置,bin/console工作了。

【讨论】:

    【解决方案2】:

    当你添加新字段时,你应该doctrine:schema:update

    【讨论】:

    • 不,你应该使用bin/console doctrine:migrations:diff 然后bin/console doctrine:migrations:migrate
    • @Shady 仅当他使用迁移时!!!如果他有一个手动添加的表,他可能不想使用迁移
    • 他使用使用 Doctrine Migration 的 Sylius,所以......他还在他的信息中说他使用 Doctrine Migration。
    • @Shady 在 Sylius 的文档中写道:4. Update the database. There are two ways to do it. via direct database schema update: $ php bin/console doctrine:schema:update --force
    猜你喜欢
    • 2017-08-04
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 2018-09-03
    • 2018-10-16
    • 1970-01-01
    • 2022-12-25
    • 1970-01-01
    相关资源
    最近更新 更多