以下のようにテーブルのスキーマを書き込もうとしましたが、うまくいきませんでした。私が犯した間違いは何ですか?.
<?php
/*** Implementation of hook_schema()*/
function sparsh_module_schema(){
$schema['commissions_impt_data'] = array(
'description' => 'Holds the imported data from different APIs like CJ and LinkShare for the sales done.',
'fields' => array(
'r_id' => array(
'type' => 'serial',
'description' => 'The record ID',
'not null' => TRUE,
'disp-width' => '11'
),
'member_id' => array(
'type' => 'varchar',
'description' => 'Member Id for the Record(User name)',
'length' => '255',
'not null' => TRUE
),
'member_type' => array(
'type' => 'varchar',
'description' => 'Member Plane Type',
'length' => '255',
'not null' => TRUE
),
'transs_date' => array(
'type' => 'int',
'description' => 'Transaction Date',
'not null' => TRUE,
'disp-width' => '11',
'default' => 0
),
'source' => array(
'type' => 'varchar',
'description' => 'Income Source',
'length' => '50',
'not null' => TRUE
),
'merchant' => array(
'type' => 'varchar',
'description' => 'Merchant Name',
'length' => '50',
'not null' => TRUE
),
'commission_income' => array(
'type' => 'numeric',
'description' => 'Commission Income',
'precision' => '10',
'scale' => '2',
'not null' => TRUE
),
'sale_amt' => array(
'type' => 'numeric',
'description' => 'Total Sale Amt.',
'precision' => '10',
'scale' => '2',
'not null' => TRUE
),
'user_comm_perc' => array(
'type' => 'numeric',
'description' => 'User commission %',
'precision' => '10',
'scale' => '2',
'not null' => TRUE
),
'user_comm_amt' => array(
'type' => 'numeric',
'description' => 'User commission amt',
'precision' => '10',
'scale' => '2',
'not null' => TRUE
),
'dat' => array(
'type' => 'text',
'description' => 'Serialized Array of Data for the record received from the API',
'serialize' => TRUE
),
'st' => array(
'type' => 'serial',
'description' => 'status of the record.',
'not null' => TRUE,
'disp-width' => '11'
),
),
'primary key' => array('r_id'),
);
return $schema;
}
/**
* Implementation of hook_install().
*/
function sparsh_module_install(){
drupal_install_schema('sparsh_module');
}
/**
* Implementation of hook_uninstall().
*/
function sparsh_module_uninstall(){
drupal_uninstall_schema('sparsh_module');
}
//===============================================
// Helper Functions
//===============================================
function sparsh_module_write_record($table, $object) {
$schema = drupal_get_schema($table);
$fields = $values = $placeholders = array();
foreach ($schema['fields'] as $field => $info) {
// For inserts, populate defaults from Schema if not already provided
if (!isset($object->$field) && isset($info['default'])) {
$object->$field = $info['default'];
}
// Build arrays for the fields, placeholders, and values in our query.
if (isset($object->$field)) {
$fields[] = $field;
$placeholders[] = db_type_placeholder($info['type']);
if (empty($info['serialize'])) {
$values[] = $object->$field;
}
else {
$values[] = serialize($object->$field);
}
}
}
$query = "INSERT INTO {". $table ."} (". implode(', ', $fields) .') VALUES ('. implode(', ', $placeholders) .')';
return db_query($query, $values);
}
スキーマで2つの自動インクリメント列(r_id&st)を使用しました。それは間違いです。
あなたが書いたsparsh_module_write_record()
関数は、 drupal_write_record() の縮小版のようです。これは、書き込まれている行がデータベーステーブルにすでに存在する場合を考慮していません。
コードが別の行で使用されている主キーと同じ値を使用する行を保存しようとすると、エラーが発生します。