0

I know I can get a views result programatically with:

$view = views_get_view('view_name');
$view->execute();
$response1 = $view->result;

I can get this views sql query from the views ui (views->settings->show sql). If I place this query in a db_query() and get the results with:

$query = "... "; //long sql query from views ui (in my case with left_joins which reference field collections
$result = db_query($query);
$response2 = $result->fetchAll();  //different than $response1 above?

the responses returned from $response1 (from views) and $response2 (from db_query) are different, namely, the $response2 doesn't contain my field data (or at least not all of it). I'm not sure if this has anything to do with the fact that I'm using field collections. It also seems to not work when using the different fetch() functions. If I run this query through phpmyadmin it produces the same limited results as $response2 (from db_query) so I'm wondering if views runs something aside from the query. I'd like to find out what the method result() does for the views object, but I'm not sure where that is in the docs or where the function is in my views source. If someone can show me where that is, that would also be helpful.

2 Answers 2

1

Your view most likely has tags getting attached to it, like node_access. Or if you're using grouping on the View that may make a difference in the result property.

Like Nikhil suggested to add that hook somewhere in a custom module just to see what $query has in it. And if you have the Devel module installed you can pass that query like so:

dpq($query);

If it ends up being the case that you have tags being added to the views query and need to mimic that, I'd recommend switching over to the dynamic queries and doing an $query->addTag(...);

0

You could try hook_views_query_alter().

Alter the query before executing the query.

Parameters

$view: The view object about to be processed.

$query: An object describing the query.

1
  • The problem I'm having is not with the 'views' case; that works fine. I'm having problem with the db_query() directly, and in this case there is no view object to alter with the hook you're suggesting. The issue is that I'm not getting back the same amount of information compared to the case where I call the view.
    – Joe
    Commented Feb 18, 2013 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.