I have seen various threads on this issue, but none of them seem to be resolved. Please pardon me if this is a duplicate.
I have a content type called "event" and I am using the CCK Date field. The field is allowed to have multiple values defined.
I have an event that has three dates and the node appears in the view 3 times. I understand why the database is returning the node 3 times, because there are three dates associated to it on the join. But I am really looking for some way to "post process" the results array to throw out any other results with the same nid. And it would be really great if the Distinct setting would do this. A simple for loop that tracked the nids would do the trick, throwing out any duplicates.
I have attached a screenshot of what my results look like.
Here's my view
$view = new view;
$view->name = 'event';
$view->description = 'Event';
$view->tag = 'Event';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
'title' => array(
'label' => 'Title',
'link_to_node' => 1,
'exclude' => 0,
'id' => 'title',
'table' => 'node',
'field' => 'title',
'relationship' => 'none',
),
'field_subtitle_value' => array(
'label' => '',
'link_to_node' => 0,
'label_type' => 'none',
'format' => 'default',
'multiple' => array(
'group' => TRUE,
'multiple_number' => '',
'multiple_from' => '',
'multiple_reversed' => FALSE,
),
'exclude' => 0,
'id' => 'field_subtitle_value',
'table' => 'node_data_field_subtitle',
'field' => 'field_subtitle_value',
'relationship' => 'none',
),
'field_event_date_value' => array(
'label' => '',
'link_to_node' => 0,
'label_type' => 'none',
'format' => 'medium',
'multiple' => array(
'group' => 1,
'multiple_number' => '',
'multiple_from' => '',
'multiple_reversed' => 0,
),
'exclude' => 0,
'id' => 'field_event_date_value',
'table' => 'node_data_field_event_date',
'field' => 'field_event_date_value',
'relationship' => 'none',
),
'field_teaser_value' => array(
'label' => '',
'link_to_node' => 0,
'label_type' => 'none',
'format' => 'default',
'multiple' => array(
'group' => TRUE,
'multiple_number' => '',
'multiple_from' => '',
'multiple_reversed' => FALSE,
),
'exclude' => 0,
'id' => 'field_teaser_value',
'table' => 'node_data_field_teaser',
'field' => 'field_teaser_value',
'relationship' => 'none',
),
'field_event_img_fid' => array(
'label' => '',
'link_to_node' => 0,
'label_type' => 'none',
'format' => '120w_scale_linked',
'multiple' => array(
'group' => TRUE,
'multiple_number' => '',
'multiple_from' => '',
'multiple_reversed' => FALSE,
),
'exclude' => 0,
'id' => 'field_event_img_fid',
'table' => 'node_data_field_event_img',
'field' => 'field_event_img_fid',
'relationship' => 'none',
),
'view_node' => array(
'label' => '',
'text' => 'Event details >',
'exclude' => 0,
'id' => 'view_node',
'table' => 'node',
'field' => 'view_node',
'relationship' => 'none',
),
'nid' => array(
'label' => '',
'link_to_node' => 0,
'exclude' => 0,
'id' => 'nid',
'table' => 'node',
'field' => 'nid',
'relationship' => 'none',
),
));
$handler->override_option('sorts', array(
'field_event_date_value2' => array(
'order' => 'ASC',
'id' => 'field_event_date_value2',
'table' => 'node_data_field_event_date',
'field' => 'field_event_date_value2',
'relationship' => 'none',
),
));
$handler->override_option('filters', array(
'status' => array(
'operator' => '=',
'value' => 1,
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'status',
'table' => 'node',
'field' => 'status',
'relationship' => 'none',
),
'type' => array(
'operator' => 'in',
'value' => array(
'event' => 'event',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'type',
'table' => 'node',
'field' => 'type',
'relationship' => 'none',
),
'date_filter' => array(
'operator' => '>=',
'value' => array(
'value' => NULL,
'min' => NULL,
'max' => NULL,
'default_date' => 'now',
'default_to_date' => '',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'date_fields' => array(
'node_data_field_event_date.field_event_date_value2' => 'node_data_field_event_date.field_event_date_value2',
),
'granularity' => 'day',
'form_type' => 'date_select',
'default_date' => 'now',
'default_to_date' => '',
'id' => 'date_filter',
'table' => 'node',
'field' => 'date_filter',
'relationship' => 'none',
),
));
$handler->override_option('access', array(
'type' => 'none',
));
$handler->override_option('footer_format', '1');
$handler->override_option('footer_empty', 0);
$handler->override_option('items_per_page', 0);
$handler->override_option('distinct', 1);
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('fields', array(
'title' => array(
'label' => 'Title',
'link_to_node' => 1,
'exclude' => 0,
'id' => 'title',
'table' => 'node',
'field' => 'title',
'relationship' => 'none',
),
'field_event_date_value' => array(
'label' => '',
'link_to_node' => 0,
'label_type' => 'none',
'format' => 'short',
'multiple' => array(
'group' => 1,
'multiple_number' => '',
'multiple_from' => '',
'multiple_reversed' => 0,
),
'exclude' => 0,
'id' => 'field_event_date_value',
'table' => 'node_data_field_event_date',
'field' => 'field_event_date_value',
'relationship' => 'none',
'override' => array(
'button' => 'Use default',
),
),
'view_node' => array(
'label' => '',
'text' => 'Event details >',
'exclude' => 0,
'id' => 'view_node',
'table' => 'node',
'field' => 'view_node',
'relationship' => 'none',
),
'city' => array(
'label' => '',
'exclude' => 0,
'id' => 'city',
'table' => 'location',
'field' => 'city',
'override' => array(
'button' => 'Use default',
),
'relationship' => 'none',
),
'name' => array(
'label' => '',
'exclude' => 0,
'id' => 'name',
'table' => 'location',
'field' => 'name',
'override' => array(
'button' => 'Use default',
),
'relationship' => 'none',
),
));
$handler->override_option('footer', '<div class="view-all-events">
<a href="/events">View all events ></a>
</div>');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);
$handler = $view->new_display('feed', 'Feed', 'feed_1');
$handler->override_option('style_plugin', 'rss');
$handler->override_option('style_options', array(
'mission_description' => 0,
'description' => 'Events Feed | Reverend Billy & The Church of Stop Shopping',
));
$handler->override_option('row_plugin', 'node_rss');
$handler->override_option('row_options', array(
'item_length' => 'fulltext',
));
$handler->override_option('path', 'events-feed.xml');
$handler->override_option('menu', array(
'type' => 'none',
'title' => '',
'weight' => 0,
'name' => 'navigation',
));
$handler->override_option('tab_options', array(
'type' => 'none',
'title' => '',
'weight' => 0,
));
$handler->override_option('displays', array());
$handler->override_option('sitename_title', FALSE);
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->override_option('path', 'event-test');
$handler->override_option('menu', array(
'type' => 'none',
'title' => '',
'weight' => 0,
'name' => 'navigation',
));
$handler->override_option('tab_options', array(
'type' => 'none',
'title' => '',
'weight' => 0,
));
Here's the query:
SELECT DISTINCT(node.nid) AS nid,
node_data_field_event_date.field_event_date_value2 AS node_data_field_event_date_field_event_date_value2,
node.title AS node_title,
node_data_field_subtitle.field_subtitle_value AS node_data_field_subtitle_field_subtitle_value,
node_data_field_subtitle.nid AS node_data_field_subtitle_nid,
node.type AS node_type,
node.vid AS node_vid,
node_data_field_teaser.field_teaser_value AS node_data_field_teaser_field_teaser_value,
node_data_field_teaser.field_teaser_format AS node_data_field_teaser_field_teaser_format,
node_data_field_teaser.nid AS node_data_field_teaser_nid,
node_data_field_event_img.field_event_img_fid AS node_data_field_event_img_field_event_img_fid,
node_data_field_event_img.field_event_img_list AS node_data_field_event_img_field_event_img_list,
node_data_field_event_img.field_event_img_data AS node_data_field_event_img_field_event_img_data,
node_data_field_event_img.nid AS node_data_field_event_img_nid,
node_data_field_event_date.field_event_date_value AS node_data_field_event_date_field_event_date_value
FROM node node
LEFT JOIN content_field_event_date node_data_field_event_date ON node.vid = node_data_field_event_date.vid
LEFT JOIN content_field_subtitle node_data_field_subtitle ON node.vid = node_data_field_subtitle.vid
LEFT JOIN content_field_teaser node_data_field_teaser ON node.vid = node_data_field_teaser.vid
LEFT JOIN content_type_event node_data_field_event_img ON node.vid = node_data_field_event_img.vid
WHERE (node.status <> 0) AND (node.type in ('event')) AND (DATE_FORMAT(STR_TO_DATE(node_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T'), '%Y-%m-%d') >= '2008-11-02')
ORDER BY node_data_field_event_date_field_event_date_value ASC
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | export_viewscalendar.txt | 15.32 KB | twenty2nd |
| #8 | date_api_filter_handler.patch | 2.1 KB | benkewell |
| #7 | date_api_filter_handler.patch | 1.84 KB | benkewell |
| Picture 2.png | 134.8 KB | capellic |
Comments
Comment #1
merlinofchaos commentedAll CCK Fields normally have a 'group multiple values' flag which handles this. In any case, your'e talking about non core Views fields; it's up to the handlers of the module that provides the field to provide all processing. That's why there's this stern warning in the submission guidelines:
Finally, the distinct fields adds the DISTINCT keyword to the query. But some queries are simply complex enough that the database fails. I don't know why, nor is there much I can do about it. If the database doesn't actually make it DISTINCT, that's how the cookie crumbles.
Comment #2
merlinofchaos commentedComment #3
benkewell commentedi think this is the same issue as this
http://drupal.org/node/298646
the problem with "group multiple values" is that it can only make the "date field" to display only one date
but not preventing the "node" being duplicated several times in the views result due to date repeat field
Comment #4
benkewell commentedmy SQL output from views looks like this:
SELECT
DISTINCT(node.nid) AS nid,
node_data_field_date_repeat.field_date_repeat_value AS node_data_field_date_repeat_field_date_repeat_value,
node.title AS node_title FROM mcs__node node
LEFT JOIN mcs__content_field_date_repeat node_data_field_date_repeat ON node.vid = node_data_field_date_repeat.vid
WHERE (node.type in ('activity')) AND (DATE_FORMAT(STR_TO_DATE(node_data_field_date_repeat.field_date_repeat_value, '%Y-%m-%dT%T'), '%Y-%m-%d') >= '2008-11-05')
i have only chosen node title as the view's output field
but in the SQL, "node_data_field_date_repeat.field_date_repeat_value" is also output
and after executing the SQL in phpMyAdmin directly
i found that it is the field_date_repeat_value field preventing the DISTINCT to work,
i.e. once the field_date_repeat_value item is removed, the DISTINCT works properly
is the field_date_repeat_value item used for any purpose?
can an option be added in the date filter to choose whether output duplicated nodes or not?
Comment #5
benkewell commentedi investigated deeper and find that there is a difference between the sql for DISTINCT in views 5.x-1.6 and 6.x-2.1
the 5.x-1.6 SQL adds " GROUP BY node.nid " at the end of the DISTINCT query while 6.x-2.1 does not
if i add that manually to my SQL from 6.x-2.1 and execute in phpMyAdmin, the DISTINCT works correctly
so the correct SQL for the one in #4 should be
i don't know if it's a bug or a change in the new version of views
but this matches some other statements i found from Google
which said that MySQL DISTINCT doesn't work correctly if only DISTINCT is used without GROUP BY
i think this may also be why the DISTINCT not working in some other cases
as stated in the DISTINCT option description in views setting page
Comment #6
merlinofchaos commentedThis is not about DISTINCT. DISTINCT is not the answer, it is up to the handler to provide proper SQL that will not bring duplicates. DISTINCT is a highly unreliable solution and I really wish it simply didn't exist, because when it doesn't work people file bugs against Views.
The group by thing was a hack, but it has some other ugly side effects, and I chose not to reproduce it in Views 2, and I stand by that choice. Again, it is up to date.module (or CCK) to provide SQL that will not cause duplicates when it knows that there are multiple fields. This is doable, it just sucks.
Comment #7
benkewell commentedattached patch add an option to the date filter, allowing to disable date field data in the query result
and thus make the distinct function to work
in fact after inspecting the code
i think it is better to directly remove the line 430
as the "group values" function of cck date repeat field still works even if the line is removed
but produces duplicated nodes if the line is there
Comment #8
benkewell commenteda update of the old patch, which fixes the issue that the setting value is not displayed properly
Comment #9
karens commentedThis is no longer needed. I've made changes to the way the filter works that make adding the field unnecessary, so it isn't done any more.
Comment #10
karens commentedComment #11
benkewell commented1) it works with distinct without random filter
2) it works with distinct plus random filter when a node type filter is used with only one node type is selected
3) but not works with distinct plus random filter when a node type filter is used with more than one node type is selected
4) but not works with distinct plus random filter when no node type filter is used
don't know if this is related to date since it works in case 2
Comment #12
benkewell commentedbelow is the sql generated by view
i tried to modify the sql and execute it directly in phpmyadmin,
and gave the same duplicated rows output
Comment #13
techninja commentedBump-Subscribe!
I've recently come across the exact same issue, but for now I'll cope with duplicated output. Manually adding group by to the query run directly DOES seem to provide exactly what I'm looking for, but I tend to heed the warnings of the wise merlinofchaos when given, so here's hoping there's a better solution...
Comment #14
karens commentedThis should work fine now if you use the 'group' option for multiple values in the latest -dev code. I've been making fixes for the last couple days, so you need the very latest to be sure you have what I have.
Comment #15
clivesj commentedIs it also applied to the latest 5.x.dev?
In that version Views still shows repeating nodes when the nodes contains multiple dates.
Do I have to open a new issue, or do I set this one to active?
Regards
EDIT:
OK I see the D6 logic is not the same as in D6. The problem disappears when I switch back to 5.2, so I will file an issue for D5.
Comment #16
wuf31 commentedUsing Date: 6.x-2.x-dev dated March 9, 2009.
Still not working.
SQL Generated from Views:
No distinct here, is it something that I did wrong ??
Comment #17
wuf31 commentedOk, Found the distinct configuration..
So, there EXISTS Distinct now at the SQL, but still the duplicated contents being outputted :)
Any help appreciated. Any guidance on how to make the sql result not be duplicated ??
Comment #18
twenty2nd commentedUnfortunately, like wuf31, using the latest dev release for D6 Date Module didn't solve this issue in my particular case.
I'm running Drupal6.9 and using the following modules:
Date 6.x-2.0
Calendar 6.x-2.0
CCK 6.x-2.x-dev
Views 6.x-2.3
Here is the query for my "Upcoming Events Block" which still lists the node duplicates, despite using the DISTINCT function and grouping multiple date values.
I've exported and attached my views calendar settings for further review. Perhaps I just did something wrong with my settings that causes the node to still duplicate.
I really love how Date and Calendar module work! Though still very much new to Drupal, I would like to help out as much as I can in figuring out these issues.
Best Regards,
Cecille
Comment #19
GregoryHeller commentedSee also: http://drupal.org/node/302498 comments 37, 39 and 43 explain that you must be running the dev versions of cck, views, date and calendar.
I am marking as a duplicate, because it certainly seems like it is.