Posts Tagged views
Custom sql queries in Views
I came across this when i was looking for a way to use my own sqly queries in combination with views.
Sometimes you can’t always get the things you want by using views (or you’re too lazy to fiddle around with arguments, relationships, etc.)
The idea is simple :
1) test your own custom query in phpmyadmin for example.
2) create a view and save it. You don’t have to set any filters since the SQL will be overwritten.
2) if you haven’t created your own module yet, create one now and place the following code in it. replace MYMODULE by the name of your own module and VIEW_NAME by the name of the view you’ve just created. Then, change the sql query you see here by your own.
function MYMODULE _views_pre_execute(&$view) {
//drupal_set_message(’—->>>’.$view->name);
if($view->name==”VIEW_NAME”) {
$view->build_info['query']=”SELECT vidnode.nid as nid, vidnode.title as title
FROM node vidnode WHERE vidnode.type=’ptl_remotevideo’
AND vidnode.status <> 0
AND vidnode.nid NOT IN (
SELECT DISTINCT media.field_media_video_ref_nid as video_nid
FROM content_field_media_video_ref media
)”;
}
}
That’s it. I used a node view and it gave me what i wanted. I didn’t have time to check out other settings but i guess it shouldn’t be too hard.
Recent Comments