Lavorare con db esterno a Drupal

2 contenuti / 0 new
Ultimo contenuto
Lavorare con db esterno a Drupal
AllegatoDimensione
Image icon Immagine.png7.54 KB

Ciao a tutti,
sto sviluppando un modulo il quale seleziona dei dati da una sorgente esterna a Drupal.
Dapprima ho configurato il file settings.php come segue:
$databases = array();
$databases['default']['default'] = array(
'database' => 'drupaldb',
'username' => 'drupal',
'password' => 'xxx',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
);
$databases['external_db']['default'] = array(
'database' => 'xxx',
'username' => 'xxx',
'password' => 'xxx',
'host' => '194.6.184.52',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
);

Questo invece è il modulo:

<?php
function enable_user_web_menu() {
 
$items = array();
 
$items['examples/test'] = array(
   
'title' => 'Enable web user',
   
'description' => 'Enable web user',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('enable_user_web_form'),
   
'access callback' => TRUE
 
);
  return
$items;
}
function
enable_user_web_form($form, &$form_state) {
$form['cod_customer'] = array(
   
'#type' => 'textfield',
   
'#title' => 'Codice cliente',
   
'#size' => 10,
   
'#maxlength' => 5,
   
'#required' => TRUE,
  );
 
$form['submit_button'] = array(
   
'#type' => 'submit',
   
'#value' => t('Abilita'),
  );
  return
$form;
}
function
enable_user_web_form_validate($form, &$form_state) {
if (!(
$form_state['values']['cod_customer'] > 0)){
   
form_set_error('cod_customer', t('Must be a number.'));
  }
}
function
enable_user_web_form_submit($form, &$form_state) {
drupal_set_message($form_state['values']['cod_customer']);
try {
$uid=12345;
db_set_active($key='external_db');
$result = db_query('SELECT name FROM {customer} WHERE customercode  = :uid',array(':uid' => $uid));
foreach (
$result as $record=>$name) {
print
$result->name;
}
}
catch (
PDOException $e) {
 
print_r($e->getMessage());
}
db_set_active();
}
?>

Il problema è che quando richiamo il metodo db_set_active($key='external_db') all'interno del enable_user_web_form_submit() sembrerebbe che non riesca a fare lo switch del DB.

La mia domanda è la seguente: dove posso definire, all'intrno del mio modulo, il db_set_active?
Come detto prima, ora è all interno del enable_user_web_form_submit()..... ma non penso sia il posto corretto.....
In allegato il messaggio d'errore.

Vi ringrazio tantissimo per un eventuale supporto.

Saluti,
Pagi

Drupal Version:

Mi sa che ho sollevato un putiferio per niente.... errore mio:

foreach ($result as $record=>$name) {

}

foreach ($result as $record) {

}