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

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)

Wow someone with an actual views api question. cool! Your code looks fine in general.


      'title' => t('ID'),

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?

ursula’s picture

My 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

ursula’s picture

I 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

dawehner’s picture

I'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.

ursula’s picture

Too 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

dawehner’s picture

Status: Postponed (maintainer needs more info) » Fixed

Cool idea.

Let's mark this issue was fixed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Mołot’s picture

Status: Closed (fixed) » Active

Sorry 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

It can already support external databases, but there is absolutely no way there will ever be a UI that lets you enter database and table names.

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.

Mołot’s picture

Status: Active » Needs review
Mołot’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.