Domanda

Ho provato a scrivere lo schema per un tavolo come indicato di seguito, ma non ha funzionato. fatemi sapere cosa è l'errore che ho fatto.

<?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);
}
È stato utile?

Soluzione

ho usato due colonne autoincremento nello schema (R_ID & st); questo è l'errore.

Altri suggerimenti

La funzione sparsh_module_write_record() hai scritto sembra una versione ridotta di drupal_write_record () che non considera il caso della riga in fase di scrittura è già presente nella tabella del database.
Se il codice tenta di salvare una riga che utilizza lo stesso valore della chiave primaria utilizzata da un'altra riga, quindi si ottiene un errore.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a drupal.stackexchange
scroll top