I have for a module I created, to maintain member data for a karate club, a custom views_handler_field with as query:
function query() {
$this->ensure_my_table();
$this->add_additional_fields();
$this->field_alias = $this->table . '_' . $this->field;
$this->query->add_groupby($this->aliases['vid']);
$join = new views_join();
$join->construct('clubmember_belt', $this->table_alias, 'vid', 'vid');
$this->belts_table = $this->query->ensure_table('clubmember_belt', $this->relationship, $join);
$this->query->add_field(NULL, "MIN($this->belts_table.gid)", $this->field_alias);
}
the reason for this query is to grab, from a related table, the entry where clubmember_belt.gid has the lowest number....
This creates, in views, the following query
SELECT node.nid AS nid,
clubmember.vid AS clubmember_vid,
MIN(clubmember_belt.gid) AS clubmember_first_belt
FROM node node
LEFT JOIN clubmember clubmember ON node.vid = clubmember.vid
LEFT JOIN clubmember_belt clubmember_belt ON clubmember.vid = clubmember_belt.vid
WHERE (node.status <> 0) AND (node.type in ('clubmember'))
GROUP BY clubmember_vid, nid, clubmember_first_belt
This query however has an error: Can't group on 'clubmember_first_belt'
This code used to work on an older version of Views2 but since going to the latest version it fails.
I see no way to remove the 'clubmember_first_belt' from the group by list. If I do this manually the query works just fine....
I did a rewrite by adding the clubmember_first_belt via a pre_render function and that works but then I can not get click_sort to work correct.
When I change the code in 'views_plugin_query_default.inc' I can get it to work correct. See patch.
| Comment | File | Size | Author |
|---|---|---|---|
| filename.patch | 802 bytes | bwynants |
Comments
Comment #1
merlinofchaos commentedYou have to add the field with the aggregate argument set to true, then it will not add it to the group by. additional_fields() doesn't have a mechanism to do that, so you are going to need to add the field with add_field() instead.
Comment #2
bwynants commented>so you are going to need to add the field with add_field()
isn't that what I was doing?
i changed the line add_field to (cfr views_handler_filter_search.inc)
$this->query->add_field('', "MIN($this->belts_table.gid)", $this->field_alias, array('aggregate' => TRUE));
but this does not help?
Can you please give some more info?
Thanks in advance.
Comment #3
dawehnerThis patch is for 3.x so you would have to redo you patch, because group by support was commited
Comment #4
bwynants commentedi'm at 3.0 but have no clue what I should be changing to my code. can you give a clue? is there an example?
Comment #5
dagmarYou can see the new handlers here: http://drupal.org/cvs?commit=287316
Comment #6
bwynants commentedHow can this be working?
The file views_handler_argument_group_by_numeric.inc contains a class named views_handler_argument shouldn't this class be called views_handler_argument_group_by_numeric
Comment #7
dagmarSo, this is not a bug.
Comment #8
bwynants commentedOk
I changed my code to
And this seems to work fine now.
PS: The patch in this issue is irrelevant in 3.0!
Comment #9
merlinofchaos commentedWell, #6 is clearly a bug. Checking in a fix for that. =)
Comment #10
bwynants commentedthx, and thanks also for the help, #1 works fine with the latest 3.0 code