2012-09-03 32 views
1

我有這樣的實體:Drupal的,修改enity

$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,所以我手動修改它。但是現在當我嘗試保存新字段未填充的實體時。我已經安裝了devel模塊,並使用'drush cc all'清除緩存,取消激活並激活了模式但仍不起作用

回答

0

這是更新腳本的用途。你會想要寫一個update_N function,在其中您可以使用the schema objectadd your column

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將相應地修改您的表。