1

I tried to write the schema for a table as given below, but it did not work. let me know what is the mistake i have made.

<?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);
}
1
  • What does not work? Are there errors? "It does not work" is not a useful error description in any way.. – Berdir Jun 24 '11 at 11:15
2

I used two autoincrement columns in the schema (r_id & st); that is the mistake.

1

The sparsh_module_write_record() function you wrote seems a reduced version of drupal_write_record() that doesn't consider the case the row being written is already present in the database table.
If the code tries to save a row that uses the same value of the primary key used by another row, then you get an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.