Hi there,
Not sure whether to post this here or in the fora, went for the double-post, please delete if this is inappropriate here.
I'm currently porting my website into Drupal, and have an assembly-kit management in it. Functionality-wise, the following happens: Users can define a list of items they own, and at the same time there is a database of project, listing instructions and the required items to build them. Now, what I want it a display of which projects a user can attempt based on his inventory, and for which he needs to buy additional items first.
The query for this uses a negative check, counting the items a user needs in addition to what he has. $uInventory is a string with all the item IDs from his inventory, and $difference is the amount of additional items required that should be shown.
$query=<<<ENDE
SELECT project, COUNT(DISTINCT item) AS Num
FROM rel_pro_item
WHERE item NOT IN ($uInventory)
GROUP BY project
HAVING Num = $difference
ENDE;There is a dummy item in every project and in no inventory to make this query work, so $difference has to be at least one, then showing all projects that a user can attempt with his current inventory.
OK, so much for the background. Basically, what I'm trying to do now is to port this query into views as a filter. Projects are nodes, the inventory management is in a separate table, not organised as nodes.
Does this make sense so far? (If not, please drop a short line, and I'll try to explain better). If you understand what I'm trying to do: Is there any way to achieve this using views (and filters?) and if so, what do I have to do. $query->add_where does not seem to be sufficient here?
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | having_0.patch | 1.66 KB | kscheirer |
Comments
Comment #1
merlinofchaos commentedCongratulations! You're the first person to request support for the HAVING clause.
Currently Views doesn't support it. If you look at views_query.inc you'll see the various bits and pieces the query object supports. add_having is something I never had call to put in, since Views doesn't do a lot with GROUPing. There are a bunch of odd things you have to do to properly group and the views query object isn't even a little bit smart about that. So it gets complex.
Patches are welcome.
Comment #2
dkruglyak commented+1. The requests for grouping actually come up pretty often, usually to group nodes by a taxonomy term. There are handbook pages somewhere talking about how to do this in the theming layer. I have one of those things in my site.
However, this really should be supported in Views query engine and admin UI... I think this would be popular if/when available.
Comment #3
karlmoritz commentedThanks for the quick reply. I'll have a proper look at the views code tomorrow, but from what I gather, I'd have to add far more than simply an add_having to allow for a query as the one I described (what about the other fun (count, where not in, ...), so a standalone solution will probably come a lot easier.
Comment #4
mbria commentedI'm working on a jpgraph integration module and seams obvious I need to make it "views compliant". :-)
I played a little with view's header and footer, and results are promising, but HAVINGs are the perfect solution to quantify the results, for instance, based on a taxonomy term.
I took a look to Karen's view_calc but it neither fit.
I managed in the specific cases required by my project but looks like I'm not good enough programmer to patch your module with the HAVINGs extension.
So are you thinking in include this in future releases? Any suggestion to extend views with graph support.
Thanks a lot for your help and for the wonderful views module.
Comment #5
kscheirerHmm, turns out I am now interested in this too :)
anytime you want to add a filter based on a calculated field (like a COUNT), this would be handy. I'm mostly only familiar with MySQL, are there any concerns regarding portability of this kind of statement?
I found this on the mysql docs:
looks like we could do something very simple like the add_groupby(), and insert the having clause between "group by" and "order by".
The supplied patch is very basic, and does no error checking, but works, and could be a starting point if anyone wants to add to it.
My testing has only been the 1 use case I have so far, but it worked out.
Comment #6
dkruglyak commentedQuery engine mod looks like a very good start.
What do you think is the best way to expose add_having through the API / UI ?
Comment #7
kscheirerwell, I dont think it will change anything for the UI.
But it already adds the add_having() method to the API,
so when module writers expose their variables to views, they can use a standard filter handler, and call that method in their handler.
so for ex, currently one would have something like...
so that would set up your table, and make available a field that counts the number of rows and returns it. so far so good. But there's no way to add it to filters, since you need a HAVING clause, and a WHERE just won't do. So with the attached patch:
So the filter values and operators are still selectable in the UI as normal, the only change is the handler calls the new add_having() method, and later when the query is run, the HAVING clause is inserted into the SQL statement.
Comment #8
dkruglyak commentedYes, this example is helpful. Any chance you could you post sample code for a completed implementation?
The reason I brought up the API issue is because HAVING is dependent on GROUP BY, which makes filters dependent on fields. This also does not solve the challenge of grouping in the output - now requiring theme hacking.
Maybe in addition to fields/filters we need a separate equal-level entity called "groups" to integrate group-able fields, filtering groups by having clause and recognizable by views plugins for intelligent theming (default output grouping behavior).
This seems like an obvious next step to me.
Comment #9
kscheirerThe example I posted is very basic, and does no error checking, but is functional. Could you explain a little more about what a completed implementation means to you?
Comment #10
dkruglyak commentedCase in point - I want to output nodes grouped by taxonomy (from specific vocab). This means that instead of a single node list (or table or whatever), I want to generate a list of groups (per taxonomy) with each of those groups comprising a list of nodes.
Right now I have to do this grouping by hacking the view theming function, which is error prone, redundant and hard to maintain. A better approach would be to have a Views API / UI for generating "lists of groups of nodes" instead of just a "lists of nodes".
This could include several things: 1) Picking which field(s) to use for grouping; 2) Picking which filters to use on groups (that's where having clause comes in); 3) Sorting groups (which could be different from sorting nodes); 4) Calling different theming functions to theming each group vs. output list of these groups; 5) Good theming defaults for group handing in standard view types (table, list, etc).
So this is about extending views / views_ui to replace custom hacking of theming functions.
Comment #11
kscheirerAh, thank you. I think we are talking about 2 completely separate things in this case. The use of a MySQL GROUP BY clause doesn't really do what it might sound like (grouping a bunch of nodes by taxonomy term for example). All it really does is tell MySQL what fields it should use when calculating aggregate functions. And my patch is really just concerned with adding a HAVING clause to the query, I don't think it is useful for your purpose at all.
Comment #12
dkruglyak commentedThese things are separate but related.
Whatever goes into GROUP BY clauses could/should be recognized by and interact with management of node groups.
Your patch is still one of the first steps to smarter group handing.
Comment #13
xjmTracking.
Comment #14
kscheirerI don't think anything is happening with this thread. And Views has undergone some major changes since these patches were discussed, so none of this thread is likely to apply. should we close this issue and open a new one if there's still need/demand?
Comment #15
sunSince this would not affect existing implementations I have only one issue with this patch:
We can do better here, since Views already provides the corresponding functions to check whether a field or table has been added.
Also, I need at least one confirmation from someone who did not have this patch applied on a larger Views-based Drupal site to be RTBC.
Comment #16
sunAlso, better title.
Comment #17
dergachev commentedThanks for the patch, it's exactly what I was about to cobble together for my patch of location 5.x-3.x-dev.
It might also be a good idea to have the notion of "having_args", to be consistent with "where_args".
For example, add_having should support the equivalent of this:
Comment #18
dergachev commentedIn implementing this, I just got bitten by another bug. http://drupal.org/node/290132
My calculated distance field was not being pulled into the count query.
Views was trying to optimize the count query by not including any fields, even if there's a GROUP BY clause.
This was fixed in D6 but not D5.
Comment #19
esmerel commentedNo changes are being made to 1.x at this point.
Comment #20
panchoStill no HAVING support in Views.
Another good reason is that we have to repeat whole complex expressions in a WHERE clause, just because WHERE doesn't support aliases, while HAVING does.
could be:
Same holds of course for aggregate functions. By the official SQL standard, I even think it's only allowed for aggregates, so we might also consider a CTE, which we currently aren't supporting either. D'oh!
Patch is over 10 years old and against Views 6.x-1.x, so marking active. There's however no valid reason to close this perfectly valid feature request just because a particular branch is no longer supported.