Question

J'ai essayé d'écrire le schéma d'une table comme indiqué ci-dessous, mais cela n'a pas fonctionné. laissez-moi savoir quel est l'erreur que je l'ai fait.

<?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);
}
Était-ce utile?

La solution

J'ai utilisé deux colonnes d'auto-incrémentation dans le schéma (R_ID & st); qui est l'erreur.

Autres conseils

La fonction sparsh_module_write_record() que vous avez écrit semble être une version réduite de drupal_write_record () qui ne considère pas le cas de la ligne en cours d'écriture est déjà présent dans la table de base de données.
Si le code tente d'enregistrer une ligne qui utilise la même valeur de la clé primaire utilisée par une autre ligne, vous obtenez une erreur.

Licencié sous: CC-BY-SA avec attribution
Non affilié à drupal.stackexchange
scroll top