Skip to main content
added 89 characters in body
Source Link
mikeytown2
  • 8.3k
  • 1
  • 33
  • 46

Get query without running view->execute. Wrapped in a function with the _fast suffix.

function get_view_query_string_fast($view_name, $view_display, $pager = TRUE) {
  // Get view and set display.
  $view = views_get_view($view_name);
  $view->set_display($view_display);

  // Disable Pager.
  if (!$pager) {
    $pager = $view->display_handler->get_option('pager');
    $pager['type'] = 'none';
    $view->display_handler->set_option('pager', $pager);
  }

  // Get query object.
  $view->pre_execute();
  $view->build();
  $query = $view->build_info['query'];
  $query->addMetaData('view', $view);
  $query->preExecute();

  // Get the query string, ready to run.
  $query_string = (string)$query;
  $query_string = str_replace('{', '', $query_string);
  $query_string = str_replace('}', '', $query_string);
  $query_params = $query->getArguments();
  foreach($query_params as $placeholder => $value) {
    if(!is_numeric($value)) {
      $query_string = str_replace($placeholder, "'$value'",    $query_string);
    }
    else {
      $query_string = str_replace($placeholder, $value, $query_string);
    }
  }
  return $query_string;
}

$sql_a = get_view_query_string_fast('frontpage', 'page', TRUE);

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:page' AS view_name
FROM 
node node
WHERE (( (node.promote <> 0) AND (node.status = 1) ))
ORDER BY node_sticky DESC, node_created DESC
LIMIT 10 OFFSET 0

$sql_b = get_view_query_string_fast('frontpage', 'page', FALSE);

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:page' AS view_name
FROM 
node node
WHERE (( (node.promote <> 0) AND (node.status = 1) ))
ORDER BY node_sticky DESC, node_created DESC
function get_view_query_string_fast($view_name, $view_display, $pager = TRUE) {
  // Get view and set display.
  $view = views_get_view($view_name);
  $view->set_display($view_display);

  // Disable Pager.
  if (!$pager) {
    $pager = $view->display_handler->get_option('pager');
    $pager['type'] = 'none';
    $view->display_handler->set_option('pager', $pager);
  }

  // Get query object.
  $view->pre_execute();
  $view->build();
  $query = $view->build_info['query'];
  $query->addMetaData('view', $view);
  $query->preExecute();

  // Get the query string, ready to run.
  $query_string = (string)$query;
  $query_string = str_replace('{', '', $query_string);
  $query_string = str_replace('}', '', $query_string);
  $query_params = $query->getArguments();
  foreach($query_params as $placeholder => $value) {
    if(!is_numeric($value)) {
      $query_string = str_replace($placeholder, "'$value'",    $query_string);
    }
    else {
      $query_string = str_replace($placeholder, $value, $query_string);
    }
  }
  return $query_string;
}

$sql_a = get_view_query_string_fast('frontpage', 'page', TRUE);

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:page' AS view_name
FROM 
node node
WHERE (( (node.promote <> 0) AND (node.status = 1) ))
ORDER BY node_sticky DESC, node_created DESC
LIMIT 10 OFFSET 0

$sql_b = get_view_query_string_fast('frontpage', 'page', FALSE);

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:page' AS view_name
FROM 
node node
WHERE (( (node.promote <> 0) AND (node.status = 1) ))
ORDER BY node_sticky DESC, node_created DESC

Get query without running view->execute. Wrapped in a function with the _fast suffix.

function get_view_query_string_fast($view_name, $view_display, $pager = TRUE) {
  // Get view and set display.
  $view = views_get_view($view_name);
  $view->set_display($view_display);

  // Disable Pager.
  if (!$pager) {
    $pager = $view->display_handler->get_option('pager');
    $pager['type'] = 'none';
    $view->display_handler->set_option('pager', $pager);
  }

  // Get query object.
  $view->pre_execute();
  $view->build();
  $query = $view->build_info['query'];
  $query->addMetaData('view', $view);
  $query->preExecute();

  // Get the query string, ready to run.
  $query_string = (string)$query;
  $query_string = str_replace('{', '', $query_string);
  $query_string = str_replace('}', '', $query_string);
  $query_params = $query->getArguments();
  foreach($query_params as $placeholder => $value) {
    if(!is_numeric($value)) {
      $query_string = str_replace($placeholder, "'$value'",    $query_string);
    }
    else {
      $query_string = str_replace($placeholder, $value, $query_string);
    }
  }
  return $query_string;
}

$sql_a = get_view_query_string_fast('frontpage', 'page', TRUE);

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:page' AS view_name
FROM 
node node
WHERE (( (node.promote <> 0) AND (node.status = 1) ))
ORDER BY node_sticky DESC, node_created DESC
LIMIT 10 OFFSET 0

$sql_b = get_view_query_string_fast('frontpage', 'page', FALSE);

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:page' AS view_name
FROM 
node node
WHERE (( (node.promote <> 0) AND (node.status = 1) ))
ORDER BY node_sticky DESC, node_created DESC
Post Undeleted by avpaderno
Post Deleted by avpaderno
Source Link
mikeytown2
  • 8.3k
  • 1
  • 33
  • 46

function get_view_query_string_fast($view_name, $view_display, $pager = TRUE) {
  // Get view and set display.
  $view = views_get_view($view_name);
  $view->set_display($view_display);

  // Disable Pager.
  if (!$pager) {
    $pager = $view->display_handler->get_option('pager');
    $pager['type'] = 'none';
    $view->display_handler->set_option('pager', $pager);
  }

  // Get query object.
  $view->pre_execute();
  $view->build();
  $query = $view->build_info['query'];
  $query->addMetaData('view', $view);
  $query->preExecute();

  // Get the query string, ready to run.
  $query_string = (string)$query;
  $query_string = str_replace('{', '', $query_string);
  $query_string = str_replace('}', '', $query_string);
  $query_params = $query->getArguments();
  foreach($query_params as $placeholder => $value) {
    if(!is_numeric($value)) {
      $query_string = str_replace($placeholder, "'$value'",    $query_string);
    }
    else {
      $query_string = str_replace($placeholder, $value, $query_string);
    }
  }
  return $query_string;
}

$sql_a = get_view_query_string_fast('frontpage', 'page', TRUE);

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:page' AS view_name
FROM 
node node
WHERE (( (node.promote <> 0) AND (node.status = 1) ))
ORDER BY node_sticky DESC, node_created DESC
LIMIT 10 OFFSET 0

$sql_b = get_view_query_string_fast('frontpage', 'page', FALSE);

SELECT node.sticky AS node_sticky, node.created AS node_created, node.nid AS nid, 'frontpage:page' AS view_name
FROM 
node node
WHERE (( (node.promote <> 0) AND (node.status = 1) ))
ORDER BY node_sticky DESC, node_created DESC