Supporting Views Calc

kratib - September 13, 2009 - 19:33
Project:Views Custom Field
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:reviewed & tested by the community
Description

Views Calc is a handy module that computes aggregate functions on Views results. The way it works is by creating a new SQL statement that adds the aggregate functions to the existing Views SQL query. Unfortunately, this approach means that custom fields cannot be aggregated, since they don't exist in the database.

Attached is a patch for Views Custom Field that allows Views Calc to calculate custom fields. Please apply the patch and try it before I commit the changes to the 6.x-1.x-dev branch. To try it, you must be familiar with Views Calc. Then:
* Clear your views cache
* Instead of the "Views Calc Table" view style that it normally used with Views Calc, use the new "Views Custom Field Calc Table" style
* Using this new style, you can choose custom fields to be aggregated as you would do with normal fields

#1

kratib - September 13, 2009 - 19:34

And here's the patch :-)

AttachmentSize
views_calc.patch 10.65 KB

#2

obrienmd - November 17, 2009 - 20:23

Did this patch get committed? I have a bunch of failures when trying to run patch against current (as of today) 6.x-1.x-dev... And after patch was run, tons of errors in any views_calc view. Because I'm an idiot, I didn't attempt to just use dev and see if it was in there...

web3@web2:~/web/sites/all/modules/views_customfield$ patch -p0 < views_calc.patch
patching file views_customfield.views.inc
Hunk #1 FAILED at 68.
1 out of 1 hunk FAILED -- saving rejects to file views_customfield.views.inc.rej
patching file includes/views_customfield_calc_table.inc
patching file includes/views_customfield_handler_field_phpcode.inc
Hunk #1 FAILED at 26.
Hunk #2 succeeded at 33 with fuzz 2 (offset -8 lines).
Hunk #3 FAILED at 133.
Hunk #4 succeeded at 80 (offset -91 lines).
Hunk #5 succeeded at 93 (offset -91 lines).
Hunk #6 FAILED at 109.
3 out of 6 hunks FAILED -- saving rejects to file includes/views_customfield_handler_field_phpcode.inc.rej

I'm heading out of office now, but would be happy to test further when I get back this afternoon.

#3

obrienmd - November 17, 2009 - 21:03

OK - back in office. I'm officially an idiot - everything is functional. However, I'm getting an odd MYSQL warning:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT nid FROM comments WHERE cid= in /var/www/clients/client1/web3/web/sites/all/modules/views_customfield/includes/views_customfield_handler_field_phpcode.inc(205) : eval()'d code on line 2.

When my computed field is running the php:

<?php
$a
= db_result(db_query("SELECT nid FROM comments WHERE cid=" . $data->cid));
$b = db_result(db_query("SELECT case_type_id FROM casetracker_case WHERE nid=" . $a));
$c = db_result(db_query("SELECT case_state_name FROM casetracker_case_states WHERE csid=" . $b));
print
$c;
?>

This happens everywhere else that $data->cid is called (many different views custom fields), but they all DO produce the expected output, so $data->cid is obviously not returning null, as the SQL warning seems to suggest.

Thoughts?

#4

kratib - November 17, 2009 - 21:33

I'll review the patch for latest versions of Views Custom Field + Views Calc.

Concerning your query warnings, can you dsm($data->cid) to make sure the values are what you're expecting? And wouldn't it be better to make just one query with a couple of JOINs ?

#5

obrienmd - November 17, 2009 - 23:07

Hehe... I know nothing about databases, but if you wanted to help me w/ the joins... :)

The table actually shows the correct data, which makes the warnings even stranger.

#6

kratib - November 18, 2009 - 01:38
Status:needs review» reviewed & tested by the community

The patch applies to the latest dev version of Views Custom Field without warnings. Please confirm.

As for the joined query, something like:

<?php
$name
= db_result(db_query("SELECT ctcs.case_state_name FROM {comment} c LEFT JOIN {casetracker_case} ctc ON c.nid = ctc.nid LEFT JOIN {casetracker_case_states} ctcs ON ctc.case_type_id = ctcs.csid WHERE c.cid = %d", $data->cid));
?>

But that, of course, doesn't solve the weird warning that you got :-)

In any case, as this issue is concerned with the Views Calc patch, we can mark this as RTBC unless anyone else encounters a problem.

#7

obrienmd - November 18, 2009 - 01:41

Will confirm ASAP, probably tomorrow though. Thanks for the help w/ JOIN! Maybe I should actually learn how to make an efficient SQL query some day :)

#8

obrienmd - November 21, 2009 - 22:11

Apologies for delay - this patch works great against current -dev.

#9

trupal218 - November 26, 2009 - 09:27

subscribing

 
 

Drupal is a registered trademark of Dries Buytaert.