I created a views extension to add an external database. I followed the tutorial at http://larsolesen.dk/node/273 (without the extra handler), and added the external database to settings.php (and the database connection works, I am using it with other modules).
There is one (hopefully little) thing that I am missing:
Where am I expected to see the newly defined database table?
In the Views UI at "Add new view", what do I choose, and what do I expect to be added?
Did I forget to add something?
I also set the weight of the module to 11 in the system database, so that the module gets loaded before the views module, however, I am not sure that is still necessary in D7.
I also think that I didn't need hook_views_handlers, because that's not used anymore in D7.
Can't wrap my head around it ....
Added the code below.
(D7, Views3)
Any help will be greatly appreciated!
Thanks, Ursula
mydatabase_views.info
name = "MyDatabase Views"
description = "Exposes ModBase Tables to Views."
dependencies[] = views
package = "MyDatabase Views"
core = 7.x
version = "7.x-1.0-dev"
project = "mydatabase_views"
files[] = mydatabase_views.module
files[] = mydatabase_views.views.inc
mydatabase_views.module
<?php
function mydatabase_views_views_api() {
return array(
'api' => 3,
);
}
mydatabase_views.views.inc
<?php
/**
* Implementation of hook_views_data
*/
function mydatabase_views_views_data() {
$data['mydatabasetable']['table']['group'] = t('Homolog Sequences');
$data['mydatabasetable']['table']['base'] = array(
'field' => 'id',
'title' => t('ID'),
'help' => t('Autoincremented ID field, to enable views integration'),
'database' => 'mydatabase',
'weight' => -10,
);
$data['mydatabasetable']['database_id'] = array(
'title' => t('Database ID'),
'help' => t('Database ID of the homolog sequence'),
'field' => array(
'handler' => 'views_handler_field',
' click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
return $data;
}
Comments
Comment #1
dawehnerWow someone with an actual views api question. cool! Your code looks fine in general.
This is probably a quite bad name for a base table :) When you add a new view under admin/structure/views/add you will see under "show" the new base table which you can select. Once you have done this you can use views as always.
Did this actually helped you?
Comment #2
ursula CreditAttribution: ursula commentedMy bad, I thought the "title" was the title for the field (the primary key, in my case the ID field). I agree that it's a bad name for a base table, and I changed it to the proper name.
It does show up, and works now, thanks for the help!!!
Ursula
Comment #3
ursula CreditAttribution: ursula commentedI have another obstacle:
I am connecting to an database with credentials that also allow me to query (and join tables with) another database on the same server, using the same database engine.
In other modules (feeds_sql), I could just connect to the database, and then query using:
SELECT field FROM mytable m, otherdatabase.othertable t where ...;
As I understand the views api, the name of the database I connect to is always added to the query, thus making it impossible to join with tables in other databases. Is that correct, or is there a way?
I tried using:
$data['otherdatabase.synonyms']['database_id']
which gave me an error message.
Is there something I am missing, or do I need to rethink (reorganizing the external databases is unfortunately not an option).
Thanks, Ursula
Comment #4
dawehnerI'm sorry but i never did that. But you are right the table itself is always added to the query.
In theory you could even extend the views sql query plugin provided by views to support your use-case.
I'm not sure whether drupal itself supports this way of sql with dbtng.
Comment #5
ursula CreditAttribution: ursula commentedToo bad, looked a bit at the dbtng, and saw no obvious answer.
But I found a workaround: I created a mysql view in the first database (the one I defined in settings.php), that includes the data I need from the second database. I then defined the view as the base table, and voila! it works like a charm.
Thanks for your help! Ursula
Comment #6
dawehnerCool idea.
Let's mark this issue was fixed.
Comment #8
Mołot CreditAttribution: Mołot commentedSorry for reopening, but is there a way to do that, well, more automatically? I need data from outside database, different MySQL instance than my Drupal's one, and with no way to slave one to another or anything. Just... how?
I do not need it to be joinable with native Drupal's stuff.
I can easily read that data from my module, Drupal's multi-db support is good enough. Any way to provide translations? I can't find a manual for feeding views with things that are not in database.
Edit: http://drupal.org/node/261774
Seems merlinofchaos knew something we do not know. I need it for my module, so if it was possible ~4 years ago, I hope it still is, and I don't care for UI.
Comment #9
Mołot CreditAttribution: Mołot commentedThis seems a solution:
http://chrislarson.me/blog/drupal-and-external-mysql-database
Comment #10
Mołot CreditAttribution: Mołot commented