execute_count_query(&$count_query) on line 140 is causing a PDO error in my module when it queries the 'default' database, rather than respecting the 'database' key returned from hook_views_data().

In [949526] a similar problem was solved for the main query, and I know that my hook_views_data array is fine because the view displays properly when I turn the pager off.

I suspect a patch would be rather simple if I knew where $count_query was created, but I'm unfamiliar with Views internals.

Comments

threewestwinds’s picture

Ok, a bit more searching has revealed that in views_plugin_query_default.inc, line 1149
$this->view is NULL
when build() creates the count_query (which is later called and causes the error).

query() looks for view->base_database at line 1149, but can't find it because view is NULL at that point. So we end up looking at the default database for a table that doesn't exist.

I don't know where $this->view is supposed to be set, but apparently it's happening too late in the process for count_query to access the data is needs.

dawehner’s picture

Version: 7.x-3.x-dev » 6.x-3.x-dev
Status: Active » Needs review
StatusFileSize
new503 bytes

Here is a patch for 7.x-3.x, applies against 6.x-3.x, too.

threewestwinds’s picture

Status: Needs review » Needs work

Apparently my diagnosis of the problem was not quite right, since the patch does fix what I thought was wrong, but the error still happens.

With the given patch applied, the lifecycle of $count_query is as follows (all in views_plugin_query_default):

From line 1255, build() calls $this->query(TRUE)
At line 1159 in query(), the $options variable is set properly, with $options['target'] = 'whatever'
$query is returned, and back at line 1255, build() stores it in $view->build_info['count_query']
At line 1268, execute() stores $view->build_info['count_query'] as $count_query
At 1296, $count_query is replaced by $count_query->countQuery()
At 1309, $count_query is passed to $this->pager->execute_count_query()
At line 140 of views_plugin_pager.inc, $count_query->execute() is called, causing a PDO error by querying the wrong database.

I don't know where the problem is occurring. As far as I can tell, the database is set properly, and the same object persists until it's called. #2 is definitely a step in the right direction though, since without it $options at line 1159 is an empty array instead of array('target' => 'whatever').

dawehner’s picture

Here is an additional fix to fix the real problem in d7.

dawehner’s picture

Status: Needs work » Fixed

Commited two patches to fix this issue in d7.

The patch for addting $this->view was added to d6, too.
Thanks for helping to test the fix.

Status: Fixed » Closed (fixed)

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