1

我有这个实体:

    $schema['apiuser'] = array(
        'description' => 'The base table for api_user.',
        'fields' => array(
            'apiuser_id' => array(
                'description' => 'The primary identifier for an artwork.',
                'type' => 'serial',
                'unsigned' => TRUE,
                'not null' => TRUE,
            ),
            'public_key' => array(
                'type' => 'int',
                'not null' => TRUE,
                'default' => 0,
                'description' => "Foreign key: {file_managed}.fid of user's picture.",
            )           
        ),
        'unique keys' => array(
            'id' => array('apiuser_id')
        ),
        'primary key' => array('apiuser_id'),
        );

后来我添加了一个新字段:

,
            $schema['apiuser'] = array(
    'description' => 'The base table for api_user.',
    'fields' => array(
        'apiuser_id' => array(
            'description' => 'The primary identifier for an artwork.',
            'type' => 'serial',
            'unsigned' => TRUE,
            'not null' => TRUE,
        ),
        'public_key' => array(
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
            'description' => "Foreign key: {file_managed}.fid of user's picture.",
        ),
        'user_id' => array(
            'description' => 'The primary identifier for an artwork.',
            'type' => 'int',
            'not null' => TRUE,
        )

    ),
    'unique keys' => array(
        'id' => array('apiuser_id')
    ),
    'primary key' => array('apiuser_id'),
    );

Drupal 不会将相应的表更改为 mysql,因此我手动对其进行了修改。但是现在当我尝试保存未填充新字段的实体时。我已经安装了开发模块并使用“drush cc all”清除缓存,停用并激活了模块,但仍然无法正常工作

4

1 回答 1

0

这就是更新脚本的用途。您需要编写一个update_N 函数,您可以在其中使用模式对象添加

function your_module_update_7001($sandbox) {
  // do some checking (i.e. if already exists, etc)
  // get $schema from wherever you defined it for hook_schema...
  $spec = $schema['fields']['user_id'];
  Database::getConnection()->schema()->addField('apiuser', 'user_id', $spec);
}

然后运行http://yoursite.com/update.php,drupal 将相应地修改您的表。

于 2012-09-03T16:45:38.170 回答