Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Although an external 'database' is specified in hook_views_data definition, the external database is not used by the query.
In views_plugin_query_default.inc:
// Go ahead and build the query.
$query = db_select($this->base_table, $this->base_table)->addTag('views');
I think the db_select should specify the external database in the options parameter:
// Set query options if base_database is specified
$queryOptions = isset($view->base_database) ? array('target' => $view->base_database) : array();
// Go ahead and build the query.
$query = db_select($this->base_table, $this->base_table)->addTag('views', array('target' => $base_database);
($view should be passed as an argument to function query)
Comment | File | Size | Author |
---|---|---|---|
#14 | 949526-external-db.patch | 3.07 KB | dawehner |
#12 | 949526-external-db.patch | 2.98 KB | dawehner |
Comments
Comment #1
dawehnerMh Views uses db_set_active, which should work without using the base database, right?
Comment #2
asimov6 CreditAttribution: asimov6 commentedI guess you are right. I must have set up my database configuration in settings the wrong way.
Comment #3
asimov6 CreditAttribution: asimov6 commentedThis is how I added my external database to the $databases array in settings.php. If this is how it should be done than I quess the bug exists. Tell me if I'm wrong.
In hook_views_data I specified 'dbexternal' as the database to use.
Comment #4
dawehnerI don't see any problems here. Can you debug in the views code whether "dbexternal" is there defined as variable?
Comment #5
lsolesen CreditAttribution: lsolesen commentedhaving same problem. let me know where in the views code I need to debug, and I will get right back to you.
Comment #6
dawehnerWell views uses the drupal core api.
Did you tryed out whether drupal core supports db_set_active currently?
Comment #7
lsolesen CreditAttribution: lsolesen commentedIt does. I was able to do the following:
Comment #8
lsolesen CreditAttribution: lsolesen commentedThe new database is present in the view. However, it is not being set because of the below.
Seem to have narrowed it down to the active connection not being set in the following code in includes/database/database.inc:
I have this in setting.php
As mention in http://drupal.org/node/949526#comment-3726838 it works with a regular db_query.
So I am not quite sure whether views or core has to be changed?
Comment #9
dawehnerCan you check in plugins/views_plugin_query_defaut.inc::execute whether $view->base_database is set?
and this code is executed?
Thanks for tracking down this issue!
Comment #10
lsolesen CreditAttribution: lsolesen commentedIt is set to 'vih' as expected.
Comment #11
dawehnerThen the only thing which might can happen is that db_select doesn't support the set database and you have to do it manually.
Comment #12
dawehnerThe problem is that db_set_active does only support db_query calls.
What about adding a d7wtf tag ;)
Crell says there is no usecase for this.
Comment #13
lsolesen CreditAttribution: lsolesen commented$view is never set in the query() function.
Comment #14
dawehnerOh thanks!
New version of the patch
Comment #15
lsolesen CreditAttribution: lsolesen commentedI am still pretty certain that $view can never be set in the query function?
Comment #16
dawehnerbut $this->view should be there
Comment #17
lsolesen CreditAttribution: lsolesen commentedChecking the patch again.
Comment #18
bojanz CreditAttribution: bojanz commentedNice.
Comment #19
dawehner@bojanz
Do you understand why this kind of api change is wanted?
Comment #20
bojanz CreditAttribution: bojanz commentedYes. Looks like a simple oversight, not sure why Crell was against it.
And now, 5 minutes before RC1, it's too late.
Comment #21
merlinofchaos CreditAttribution: merlinofchaos commentedOne of the problems with this patch is that by selecting only the specific View query to utilize the external database, any secondary queries run by the view will not use the external database.
That means we may have to evaluate all secondary queries.
Comment #22
dawehnerAs on irc: secondary queries are executed on pre_render,
but the db_set_active is only in the execute method of the query plugin.
So they get executed against the main database, at the moment already(in d6)
Comment #23
merlinofchaos CreditAttribution: merlinofchaos commentedSeems like that shouldn't hold this up, then.
Comment #24
dawehnerCommited it.