By phai on
Hi all,
my need is to group by the rendering results of a view.
I find views_pre_execute and write the code:
function my_utility_views_pre_execute(&$view) {
if($view->name=="lista_rassegna_stampa") {
$view->query->add_groupby("nid");
}
}But it doesn't seem to work (my "group by" is ignored in the final query).
So I read the modify with "group by" doesn't work, and that someone used a string replace solution.
So I try:
function my_utility_views_pre_execute(&$view) {
if($view->name=="lista_rassegna_stampa") {
$search = array('ORDER BY');
$replace = array('GROUP BY nid ORDER BY');
$view->build_info['query'] = str_replace($search,
$replace,
$view->build_info['query']);
$view->build_info['count_query'] = str_replace($search,
$replace,
$view->build_info['count_query']);
}
}But in this way PHP give me an error:
Fatal error: Call to a member function addMetaData() on a non-object in ./sites/all/modules/views/plugins/views_plugin_query_default.inc on line 1306
Can you help me to add this grouping to my view? Any idea'll be really apreciated.
Really thanks
Comments
Really noone needed to group
Really noone needed to group by the results by a view?
Wow... finally I found the
Wow... finally I found the solution.
This problem seems be related to the new DB Layer introduced in Drupal7, as you can read here:
http://drupal.org/node/1127338
BTW you only need to the following line (and another for 'query_count'):
$view->build_info['query']->groupBy('field_data_field_category_field_category_value');Aggregation - how?
This alleviated the error messages but did not produce expected results (removing duplicates).
I'm not a db expert, but AFAIK know the GROUP BY statement work only for fields that are among other aggregated fields. SO how do you do that: you turn Aggregation globally on on the Views UI side, or you alter the query in the code as well?
This code would be helpful maybe
I wrote this for my custom module... tried to keep it as general as possible...
Will modify for my new module and simplfy then
hoping helps someone
how can I improve and make it more simple that code?
I dont think its good enough.. ita tpl code for generating table and gets data from view for grouped and agrrgeted tables.
Another approach how I managed to do some stuff
Coding example may help people.. that how I learn basicly