Frage

Ich habe versucht, das Schema für eine Tabelle wie unten angegeben zu schreiben, aber es hat nicht funktioniert. Lassen Sie mich wissen, was der Fehler ist, den ich gemacht habe.

<?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);
}
War es hilfreich?

Lösung

Ich habe zwei automatische Spalten im Schema verwendet (r_id & st); Das ist der Fehler.

Andere Tipps

Das sparsh_module_write_record() Die Funktion, die Sie geschrieben haben, scheint eine reduzierte Version von Drupal_Write_Record () Dies betrachtet den Fall, in dem die Zeile geschrieben wird, nicht in der Datenbanktabelle vorhanden.
Wenn der Code versucht, eine Zeile zu speichern, die den gleichen Wert der von einer anderen Zeile verwendeten Primärschlüssel verwendet, erhalten Sie einen Fehler.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit drupal.stackexchange
scroll top