【问题标题】:Drupal 7 does not save data correct after changing field type更改字段类型后 Drupal 7 无法正确保存数据
【发布时间】:2020-01-09 09:18:45
【问题描述】:

我在客户端有一个 Drupal 7 应用程序。使用带有以下代码的安装文件(在 *.install 文件中)将数据库中的字段从 int 更改为 varchar(255) 后:

$spec = array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'description' => t('The ID of the promotion, found in')
  );
  db_change_field('promotion', 'promotion_id', 'promotion_id', $spec);

数据库已更新,但是当我保存一个带有promotion_id 的实体时,例如'8sf635',它只会将8 保存到数据库中。此外,当我使用“qsfd7aer”保存实体时,它将在数据库中保存 0。

谁能告诉我这是怎么发生的?

当检查实体的保存代码时,它看起来像:

entity_save(self::ENTITY_NAME_PROPOSITION_PROMOTION, $promotion);

并且在 $promotion 对象中,promotion_id 仍然是完整的字符串。

表的架构似乎没有更新。 运行 drupal_get_schema('dpp_bab_promotion', true) 时,它将字段显示为 int 类型。

【问题讨论】:

  • 您必须将架构更改为 .intall 文件,使用 api.drupal.org/hook_schema_alter 之类的钩子并清除注册表和实体缓存您可以在此处查看 db_change_field 的使用情况api.drupal.org/api/drupal/includes!database!database.inc/…
  • 第一个 sn-p 来自 .install 文件。我使用 drush 清除了寄存器和所有缓存,但没有工作。这还有其他原因吗?数据库更改已完成,所有外键也已创建。
  • 您能否验证数据库中的字段类型是否确实发生了变化?
  • 数据库中的字段类型发生了变化。 Drupal 中的架构未更新。

标签: drupal drupal-7


【解决方案1】:

有几个可能的原因。首先是您没有清除缓存,以便新架构生效。您说您清除了缓存,所以第二个更可能是您没有将更改添加到架构中,而只是更新了数据库。听起来架构仍然认为它是一个 int。

使用 hook_schema_alter 相对容易。 API 文档的 cmets 部分有一些很好的资源: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_schema_alter/7.x

【讨论】:

  • @JimmyD 这就是我在第一条评论中解释的内容,您必须使用 hook_schema_alter 将字段结构添加到现有架构!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多