2012-08-31 27 views
0

如何將文件存儲在Drupal實體中?我有一個plublic鍵關聯到,所以我創建了一個APIuser實體的用戶,但我不知道給公鑰財產Drupal實體類中的文件屬性?

function api_user_schema() { 
    $schema['api_user'] = array(
    'description' => 'The base table for api_user.', 
    'fields' => array(
     'id' => array(
      'description' => 'The primary identifier for an artwork.', 
      'type' => 'serial', 
      'unsigned' => TRUE, 
      'not null' => TRUE, 
     ), 
     'public_key' => array(
      'description' => 'The primary identifier for the public key.', 
      'type' => ???, 
      'unsigned' => TRUE, 
      'not null' => TRUE, 
     ) 
     'created' => array(
      'description' => 
      'The Unix timestamp when the api_user was created.', 
      'type' => 'int', 
      'not null' => TRUE, 
      'default' => 0, 
     ), 
     'changed' => array(
      'description' => 
      'The Unix timestamp when the api_user was most recently saved.', 
      'type' => 'int', 
      'not null' => TRUE, 
      'default' => 0, 
     ), 
    ), 
    'unique keys' => array(
     'id' => array('id') 
    ), 
    'primary key' => array('id'), 
    ); 

    return $schema; 
} 

回答

1

你有什麼什麼樣的領域有定義一個數據庫表; Drupal不提供文件頂層,所以如果你想存儲文件,你必須手動完成。

您可以採取的最佳示例是核心用戶實體。它定義了picture屬性,這是一個引用file_managed表中的條目的ID(順便說一下,默認情況下,這是所有永久文件存儲由Drupal核心處理的方式)。

這對於數據庫列(從user_schema())架構定義:

'picture' => array(
    'type' => 'int', 
    'not null' => TRUE, 
    'default' => 0, 
    'description' => "Foreign key: {file_managed}.fid of user's picture.", 
) 

這是非常相似,你的定義需要的樣子。

從那裏,看看在user_account_form()功能(定義爲picture屬性的表單元素),以及user_validate_picture()功能,它會告訴你如何進行文件上傳,將文件保存在file_managed表,並將picture字段的提交值更改爲相關的文件ID(以便它自動針對實體進行保存)。

你將主要複製這兩個函數的代碼,所以它不會那麼棘手。