You are missing some Flash content that should appear here! Perhaps your browser cannot display it, or maybe it did not initialize correctly.
Utilizing views as a theme layer to a custom query
We had a client requirement that a single view be the combination of:
- a random list of attorneys with offices in a given state
- a random list of attorneys that are licensed in a given state.
There should be no duplications and all of list 1 must precede list 2. We could not accomplish this with views alone. After talking to Earl, we were able to access the created SQL statement within views. Here we built each of the two queries. Then, we defined a 'pre' exit and replaced the query in our view with a new one.
The new query is in the form of:
SELECT node.nid FROM ( ( select #1 ) UNION (select #2 ) ORDER BY .... ) AS node.
This is especially important in that we absolutely have to name our selected fields the same as what views would typically name them. If you don't preserve the same naming structure it will simply not work. Now this is only true for the selected fields. Fields on which you join, filter, etc can utilize any naming convention you desire. Of course, both of the select statements were required to provide the exact same column output.
So, as an example the statement looked something like: (with LEFT JOIN detail left out for easier reading)
SELECT node.nid
FROM (
( // First select statement
SELECT node.nid, RAND() as _order
FROM node node
WHERE node.attorney_state='%s'
)
UNION // concatenate to next
SELECT
( // Second select statement
SELECT node.nid, (1+RAND()) as _order
FROM node node
WHERE node.licenses_state = '%s'
)
// This does the sort on all the selects together.
ORDER BY _order
)
// Now, for Drupal, this outer select MUST be aliased as 'node'.
AS node;Making it work:
Some of the secret sauce was:
- use of an outer select with an internal union
- The outer select needed to be aliased to node for it to work.
- we only needed the nid as all the data was pulled using node-> syntax.
- There seemed to be bug in cck based on this, obviously, edge case and it took 2 lines of code to fix.
- cck expects 'vid' to absolutely have a value.
- wrapping an if ($vid)... around that expectation removed the warning error that we were getting.
- bottom line... we hacked it, but hopefully it will make sense to the cck authors and it can be submitted back to the module.
- the views hook was hook_views_pre_execute(&$view) and we altered the query directly via $view->build_info['query']. If you'd like to preserve the dynamic nature of certain aspects of the views interface, you must respect your $view->build_info['query_args']. This may require some alteration as well.
- Note that we did a RAND() and 1+RAND(). This is because RAND() returns a random decimal between 0 and 1, thus 1+RAND() would be a random decimal between 1 and 2, allowing us to order by this random number and be sure that our second query's results ALWAYS come after the first query.
I am very enjoyed for this
I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. I think it may be help all of you. Thanks a lot for enjoying this beauty blog with me. I am appreciating it very much! Looking forward to another great blog. Good luck to the author! all the best!..
This is my first time
This is my first time visiting here. I stumbled upon countless intriguing stuff within your weblog particularly the ongoing talk. From the tons of comments on your articles, I suppose I'm not the only person taking pleasure in reading your blog. Keep up the good work.
SQl is very imporant database
SQl is very imporant database language.
Views in database help us to viewa table in any way we want to, but the main table is not disturbed...sciatica remedy
lovely post
I Love such great blogs.This post is amazing.I got a lot of help from this post.
I Love such great blogs.This
I Love such great blogs.This post is amazing.I got a lot of help from this post.
<a href="http://www.maplesurveys.co.uk/">asbestos serveys London</a>
The dissertation chapters
The dissertation chapters should be perfectly composed by distinguished thesis service, when people want to present a writing talent. Thus, this is manifestly that you understand the right way to finish a superb issue referring to this topic. Thank you very much for distributing this.
I do agree with all the ideas
I do agree with all the ideas you have presented in your buy meridia 10mg post. They’re very convincing and will definitely work. Still, the posts are very short for starters. Could you please extend them a bit from next time?
Real spy gear
The dissertation chapters should be perfectly composed by distinguished thesis service, when people want to present a writing talent. Thus, this is manifestly that you understand the right way to finish a superb issue referring to this topic. Thank you very much for distributing this.
Good post. I’m been looking
Good post. I’m been looking for topics as interesting as thisVery informative and information presented very well, i really liked reading your post which has delivered a great information about how to utilize views as a theme layer to a custom query. Thanks for the code which is working very well. glass car wash buildings
Drupalcon DC Sponsorship
A proud sponsor of Drupalcon DC!

secret
The beat ingridient of the secret sauce is that you only needed the nid as all the data that was pulled using node-> syntax. Thanks.