I've been looking at many sites, answer the following situation:
Case
I need to connect Drupal Views with some external databases and these databases have the same tables.
Situation
I created one module for each external database via hook_views_data and all is ok while enabling one at a time.
Problem
I think the structure of $data overwrite each table for the different databases. See below
//MODULE FOR DATABASE1 (database1_conection)
function database1_connection_views_data {
$data['example_table']['table']['group'] = t('Example table');
$data['example_table']['table']['base'] = array(
'field' => 'id',
//...
'database' => 'database1';
);
$data['example_table']['some_field'] = array(
'title' => t('Example content'),
//...
);
//...
}
//MODULE FOR DATABASE2 (database2_conection)
function database2_connection_views_data {
$data['example_table']['table']['group'] = t('Example table');
$data['example_table']['table']['base'] = array(
'field' => 'id',
//...
'database' => 'database2';
);
$data['example_table']['some_field'] = array(
'title' => t('Example content'),
//...
);
//...
}
Possible Solutions (Obviously I'm considering these options but I do not know how can I do this yet. Or even if they are not feasible)
1. Create alias for tables (I did a test with field aliases but is not enough)
2. Create only one $data for the default external database and alter the hook_views_data dynamically and change 'database' parameter for each table
3. Create a view and then alter 'database' before make a query
4. Use an argument (contextual parameter) in a view and insert a snippet in a php code.
5. Or something easier that I do not know yet
Someone can help me?
| Comment | File | Size | Author |
|---|
Comments
Comment #1
delbozkester commentedWhen I try to create a view with more than two modules enabled I get the following error:
Creating a view

Error
Fatal error: Function name must be a string in ...\sites\all\modules\views\includes\admin.inc on line 697
Comment #2
jbrauer commentedThis seems to be related to the optgroup that is created in the wizard.
For most items in the list $form_state['values']['show']['wizard_key'] is the string name of the entity.
But if as in this case there's an optgroup in the menu the label for the optgroup is the string and the values for the items are 0, 1, 2 etc.
Without having looked I wonder if this select list should be using optgroups.
Comment #3
merlinofchaos commentedComment #1 is unfortunately unrelated to the original post.
For comment #1, only one implementation of hook_views_data is allowed to add data to a table. Any other modules implementing it must use hook_views_data_alter() to add their data to the table. See, for example, user_views_data_alter().
Comment #4
merlinofchaos commentedActually, I maybe wrong.
#1 *might* be caused by two hook_views_data() attempting to declare the same base table, and array_merge_recursive chokes on it. It looks like there is some kind of mixup between Frutas and Color there, causing a matrix which shouldn't exist.
Comment #5
Mołot commentedI think this is just a duplicate of You can't access a table in an external database with a name that is the same as one that already provided