How to Override Drupal Views Query? - drupal

How can I replace a Views 3 query with a custom SQL query in code?

From what I understand, you can use hook_views_query_alter to modify the query. I would assume you could also use it to replace the query. Here are a couple hook_views_query_alter examples, to get you started:
Using Hook Views Query Alter
Alter a View query to add a WHERE clause with OR group operator.

This might no longer be pertinent to you, but there's what seems to be a very useful discussion on Drupal.org at Implementing custom SQL query for Views / Filters that looks like it's answering my similar question.
In particular, the initial poster has suggested attaching to hook views_views_pre_execute, which someone else mentioned can be put into a custom module like:
function mymodulename_views_pre_execute(&$view) {
if($view->name=="groups_list2") {
// ...
$view->build_info['query'] = "SELECT node.nid AS nid ".
"FROM node WHERE node.type='%s'"; // wrapped for legibility
}
}
And another poster mentioned mySQL Views and Table Wizard (direct link), though they did mention it's worth bearing in mind this article about mySQL Views performance
It's definitely worth reading the whole thread on Drupal.org, though, as I found it really useful; I hope someone else does too.
Update: Indeed, this is precisely what we're now doing — overriding views_views_pre_execute in a custom module to inject new SQL before it gets to the database. I had opened a similar (but more specific) question on Drupal.SE at Slow query with large dataset in Drupal views — better to process in SQL or PHP?, which you might find useful.

If you want to execute custom SQL (that Views cannot generate), for example computed fields, or complicated SQL joins, then what you are after is a custom module.
See drupal for a guide to get you started

Related

Wordpress : Get posts but not the full WP_Post object

Does anyone know if there is a way in Wordpress's core functions to retrieve posts but not the full WP_Post object ? For example let's say only the post title and permalink ?
The full post object is very big and the standard queries quickly become slow as your site grows.
There is a return argument in the standard WP_Query :
https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter
But this only provides few options and you can't really retrieve what you want.
Thanks for your help
You can use the get_results() function. You can use simple SQL statements like SELECT x,y,z,... to get only the columns you need from a table of your choice.
Simple example:
function get_players() {
global $wpdb;
return $wpdb->get_results("SELECT a.id, a_name ROM player a");
}
The docs say that only 3 options are supported, and none of them allow an ad-hoc selection of fields. To do something more custom, you'd probably need to use the wpdb class
The most appropriate solution I found to avoid big requests is to use the WPGraphQL plugin.
It allows you to make GraphQL API calls on your Wordpress database, and directly specify in your request what you want in return. The server appears to be way less overloaded.
In my case it is very appropriate, but if you want to stay in a classic PHP wordpress approach, the get_results() function is working too as said in the other answers.

Add extra field in story content type using hook without CCK

I want to add extra field in story content type using hook, I don't want to use CCK, because am trying something different.
Please tell some suggestion with hook method.
If you do not use CCK, you will have to create your database table and code to add the form field, validate the form field, capture the data and save it in your field. I know cck can be a monster, but it does all this for you. I'd be happy to give you more info on all of this, but it is quite lengthy
There are lots of reasons that you may want to do this without CCK or Fields, and the best example is found at the node_example module in the examples project which can be found at: http://drupalcode.org/project/examples.git/tree/refs/heads/6.x-1.x:/node_example. You can also view the documentation on api.drupal.org.
The short version is that you're going to have to define your own node type using hook_node_info() and then define all the hooks for _load(), _insert(), _update(), _delete(), _access(), _validate(), and _view() in addition to defining your schema in your hook_schema and managing your tables on your own.
Sadly there is no good example for Drupal 7 as the node_example module for 7 was converted to use fields instead of the hooks listed above, which are still fully documented on api.drupal.org (they do now typically act on an array of nodes instead of a single node, but are otherwise identical).

asp.net subsonic 2.2 paging linked table collection

I'm creating a small forum for my CMS and I'm using subsonic 2.2 as my DAL.
I'm loading my theads like this:
DAL.ForumThread item = DAL.ForumThread.FetchByID(id);
In my database my ForumPosts table looks like this:
ForumPostID | ThreadID | Description | UserID | CreatedOn| etc
So now when I have my DAL.ForumThread item I can load the connected post collection by using:
item.ForumPosts();
This all works great, but the problem is that I'm using serverside paging and want to add some additional select parameters too like showing only active records.
Is this even possible when using SubSonic 2.2 ? The workaround I have now is just creating a new SubSonic.Query and select the posts by threadid and there I can set pageindex and pagesize without problems but I think this can be done easier?
I also would like to know if it makes any difference performance wise by just using item.ForumPosts() or starting a new query, I think the forumposts are already in the ForumThreads collection and don't require a new database call right?
I hope someone can point me in the right direction!
Thank you for your time and merry christmas.
Kind regards,
Mark
This goes a bit late, but. You can instead of using the Query tool use something like this:
ForumPostsCollection posts = new ForumPostsCollection().Where(ForumPosts.Columns.ThreadId,1).Load();
You can use as many parameters as you want just repeat the where clause like:
ForumPostsCollection posts = new ForumPostsCollection().Where(ForumPosts.Columns.ThreadId,1).Where(ForumPosts.Active,true).Load();
You can even use a comparison in the one of the Where methos overloads.
A for paged results, I am sure you can do it, but I dont have subsonic here with me atm, and i cannt find anything in the docos for version 2.2, there is something for v3 though.
About your second question, Subsonic uses Lazy Loading of collections, meaning that when you execute the method ForumPosts, it issues another database call to fecth that collection. The same is not true for Parent entities, the rule here is if its a method eg: ForumPosts() - A new database call is done, it it is a property eg: post.Thread it will be loaded.

Setting up Drupal node reference autosuggest to search on two separate fields

The simple versoin of my question: I need a CCK Node reference field to search on two different fields in a node. What's the best way to do this?
Some Background
I run a calendar for a physical therapy program. Each lecture has a reading list. Readings are their own content type and the lecture has a autosuggest node reference field that currently only searches the reading's title. I need it also to search on an additional cck field, the readings author.
I attempted to do this a custom view, but I need a very simple title OR author search, and we're stuck with AND for a little bit longer. Is there a workaround? Do I need to create a small module to do this? If so, wheres a good place to start to learn how to do that? (I've made custom modules before but none that involve interfacing with views/cck). I'm not really sure where to go with this right now.
Thanks for the help!
There isn't really a neat way to accomplish this, because searching only in the title is a "feature" of the Node Reference module (which is part of CCK). However, you can create a custom View to provide the results for the autocomplete, and use hook_views_query_alter() to change the query that the View execute. The view you create should be selected in the configuration page of the field.
Below is a sample implementation that changes the query to search both the title and the body of nodes. You'll probably need to customize it a little bit to get exactly what you want.
function mymodule_views_query_alter(&$view, &$query) {
if ($view->name == 'my_custom_view' && $view->current_display == 'content_references_1') {
// Remove the original title constraint
unset($query->where[0]['clauses'][2]);
// Duplicate the argument (keyword to search for), so
// it is passed to both the title and the other field
$query->where[0]['args'][] = $query->where[0]['args'][1];
// Add the custom where clause
$view->query->add_where(0, "(node.title LIKE '%%%s%%' OR node_revisions.body LIKE '%%%s%%')");
}
}

Asp.net Entity Framework and generated SQL problem

I have a problem with the following Linq query using Entity Framework:
from o in ctx.Entity
where o.EntityID = entityid
select o;
Simple enough right? Well the 'Entity' set is the parent class of a whole lot of other classes. The generated SQL for this simple query is about 20K worth of characters with a slew of 'case' and 'union'. In addition of taking a while for the framework to compile the query, it takes a while to execute too.
So how can I improve the SQL generated by the framework in case of queries using classes with heritage? Or what other technique can I use to avoid this problem?
AD
The reason it is doing that is because of the relationships of Entity with other tables in your database. To cut down on that, you need to read up on how to better control the explicit/lazy loading of references that EF is doing for you
http://blogs.msdn.com/jkowalski/archive/2008/05/12/transparent-lazy-loading-for-entity-framework-part-1.aspx
No post like this would be complete without a plug for nhibernate, which is more powerful/robust/performant/and easier to use ;-) but hopefully that link will help you out

Resources