sviluppo modulo con colonna in tabella users

5 contenuti / 0 new
Ultimo contenuto
sviluppo modulo con colonna in tabella users

ciao a tutti vorrei sviluppare un modulo che abbia delle field customizzate, mi spiego meglio, le field non devono rientrare nello schema classico di drupal 7 quindi non creando una tabella dedicata alla field nel database, ma direttamente creare una colonna in più direttamente nella tabella users. questo perchè i dati anfrebbero condivisi con un'altro software che richiede la tabella "X" con integrato delle colonna "Y", nel software no posso cambiare nulla, l'unica cosa che posso fare è appunto creare un modulo come descritto sopra.
grazie come sempre del vostro aiuto.

Drupal Version:

sto cercando di creare il file .install...
ma non capisco cosa non funzioni.
posto qui il codice:

<?php
function minelogin_schema_alter() {
 
$schema = array(
   
'type' => 'int',
   
'unsigned' => TRUE,
   
'description' => 'Field added by my_module',
  );
}
function
minelogin_install() {
 
$schema = drupal_get_schema('users');
 
db_add_field('users', 'X', $schema);
}
?>

ho risolto....
posto qui il codice, se qualcuno sa migliorarlo, ancora meglio.
ho usato una soluzione un po' casereccia, ma se usavo hook_schema_alter mi installava si le colonne, ma quando disinstallavo ill modulo mi cancellava tutte le colonne dalla tabella users

function my_schema(){
$schema = array(
      'X' => array(
      'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'the X coordinates',
      ),
      'Y' => array(
      'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'the Y coordinates',
      ),
      'Z' => array(
      'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'the X coordinates',
      ),
      'world' => array(
      'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The world of coordinates.',
      ),
      'minepass' => array(
      'type' => 'varchar',
     'length' => 32,
     'not null' => TRUE,
     'default' => '',
     'description' => 'The password of minecraft user.',
      ),
      'isLogged' => array(
      'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'minecraft user is logged?',
      ),
    );
    return $schema;
}
function minelogin_install() {
$schema = my_schema();
$keys = array_keys($schema);
foreach ($keys as $v) {
  db_add_field('users', $v, $schema[$v]);
}
drupal_set_message('minelogin successfully installed','warning');
}
function minelogin_uninstall() {
$schema = my_schema();
$keys = array_keys($schema);
foreach ($keys as $v) {
  db_drop_field('users', $v, $schema[$v]);
}
drupal_set_message('minelogin successfully uninstalled','warning');
}

per coloro servisse ho riscritto tutto e creato una nuova tabella direttamente con l'uso di $schema:

/**
* Implements hook_schema().
*/
function module_schema() {
$schema['minelogin'] = array(
    'description' => 'The base table for minelogin.',
    'fields' => array(
      'mid' => array(
        'description' => 'The primary identifier for a minelogin user.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'uid'       => array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0),
      'name' => array(
      'type' => 'varchar',
     'length' => 32,
     'not null' => TRUE,
     'default' => '',
     'description' => 'The password of minecraft user.',),
      'X' => array(
      'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'the X coordinates',),
      'Y' => array(
      'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'the Y coordinates',),
      'Z' => array(
      'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'the X coordinates',),
      'world' => array(
      'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The world of coordinates.',),
      'minepass' => array(
      'type' => 'varchar',
     'length' => 32,
     'not null' => TRUE,
     'default' => '',
     'description' => 'The password of minecraft user.',),
      'isLogged' => array(
      'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'minecraft user is logged?',),
      ),
    'foreign keys' => array(
   'users' => array(
      'table' => 'users',
      'columns' => array('uid' => 'uid'),
     )
   ),
  'primary key' => array('mid'),
  );
return $schema;
}
function module_install() {
  drupal_install_schema('minelogin');
}
/**
* Implements hook_uninstall().
*/
function module_uninstall() {
  drupal_uninstall_schema('minelogin');
}

Ciao creativo,
senza dover creare una nuova tabella puoi alterare la tabella degli utenti (attenzione!) inserendo nel tuo .install il seguente codice

<?php
/**
 * Implements hook_schema_alter().
 */
function mymodule_schema_alter(&$schema) {
 
$schema['users']['fields']['miocampo'] = array(
   
'type' => 'int',
   
'unsigned' => TRUE,
   
'description' => 'Field added by my_module',
  );
}
/**
 * Implements hook_install().
 */
function mymodule_install() {
 
$schema = array();
 
mymodule_schema_alter($schema);
 
$spec = $schema['users']['fields']['miocampo'];
 
db_add_field('users', 'miocampo', $spec);
}
/**
 * Implements hook_uninstall().
 */
function mymodule_uninstall() {
 
db_drop_field('users', 'miocampo');
}
?>