4

I need to override an SQL query of a view. I'm using Drupal 7 and Views 3.x-7.x-alpha1.

I understood I can write my own query with views_pre_execute. I followed the instructions mentioned here: http://blog.raisedeyebrow.com/2010/04/rewrite-a-views-query/.

I added this piece of code to my custom module.

function alterquery_views_pre_execute(&$view) {
  dsm($view);
}

Following the instructions, when I look for the value of $view->build_info['query'], I see that the type is object of a class SelectQuery.

How am I able to get total control of the query and use my own select statement?
I know that with hook_views_query_alter() I can modify a keyed array, but I would rather write the select statement from scratch.

  • 2
    Ìf you want to rewrite the views query from scratch, why on earth do you use views on the first place? :) – Berdir May 16 '11 at 10:01
4

Well, nothing is stoping you from building your own SelectQuery object and replacing the existing one. Overriding the query plugin is also an option. In this case it would be best to know WHY you're trying to replace the whole query. It makes very little sense to me.

Also, you're using alpha1? Why? That code is four and a half months old. Before attempting anything else, please update to -dev.

  • I think rewriting the sql is the easiest option. I can copy the sql statement from views UI and then do some modifications. So in that sense I was wrong to say I'm creating everything from a scratch. I'm also able to use views pager this way, right? – jjei May 16 '11 at 11:56
  • What are the modifications that you're trying to do? Yes, you could use the pager, but I'm wondering if there's a more targeted solution that we can suggest to you. – Bojan Zivanovic May 16 '11 at 12:32
  • One motive is that I have an exposed filter and I have to check whether the value of the this filter can be found from any of the fields present in the views. There might be other ways to do this but I'm interested to know how to modify the sql query directly. – jjei May 16 '11 at 13:44
  • @jjei you can limit the filters with results by using Views Hacks: Views Selective Exposed Filters, a D7 port is on its way. – tostinni Jun 9 '11 at 23:38
3

If you create your own views class you can overide the execute() method to do whatever you like. If you look at the apachesolr_views or sparql_views modules you can see how they do just that. The views don't even use SQL.

  • I tried this by looking sparql_views but didn't get it working. I don't get why hook views_pre_execute has been changed. There seems to be a lot of work now to use views if u want to change the actual sql statement and you haven't written views plugins before. – jjei May 17 '11 at 6:52
  • I think is is because views is a query abstraction layer, so it is designed to stop you having to write queries yourself. – Jeremy French May 17 '11 at 9:22
1

Any reason you couldn't implement hook_views_query_alter to change the query itself?

http://drupalcontrib.org/api/drupal/contributions--views--docs--views.api.php/function/hook_views_query_alter/7

  • This only let's you adjust the query object, before it's built. It's not a lot of use if you want to do something outside of Views' capabilities. – cjm2671 Jul 8 '13 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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