--- date-orig/includes/date_api_argument_handler.inc 2009-09-16 06:47:23.000000000 -0400 +++ date/includes/date_api_argument_handler.inc 2009-11-02 16:27:22.000000000 -0500 @@ -254,22 +254,28 @@ class date_api_argument_handler extends $parts = $this->date_handler->arg_parts($argument); if (empty($parts[0]['date']) && empty($parts[0]['period'])) { unset($this->view->args[$this->position]); } } - + } + + /** + * Called just prior to query(), this lets us set up table aliases for + * relationships. This can't be done in pre_query(), because the relationship + * isn't defined yet. + */ + function set_relationship() { + parent::set_relationship(); $this->get_query_fields(); if (!empty($this->query_fields)) { foreach ($this->query_fields as $query_field) { $field = $query_field['field']; - // Explicitly add this table using add_table so Views does not - // remove it if it is a duplicate, since that will break the query. - $this->query->add_table($field['table_name'], NULL, NULL, $field['table_name']); + $this->related_table_alias = $this->query->queue_table($field['table_name'], $this->relationship); } } } - + /** * Set up the query for this argument. * * The argument sent may be found at $this->argument. */ @@ -323,11 +329,11 @@ class date_api_argument_handler extends // See if we're outside the allowed date range for our argument. if (date_format($min_date, 'Y') < $this->view->date_info->min_allowed_year || date_format($max_date, 'Y') > $this->view->date_info->max_allowed_year) { drupal_not_found(); exit; } - + // The second option seems to work better in the block view if // set to something other than the original value. // Need to keep an eye on this to be sure nothing else breaks. //$format = $this->date_handler->views_formats($this->options['granularity'], 'sql'); $format = $this->date_handler->views_formats($this->granularity, 'sql'); @@ -337,18 +343,13 @@ class date_api_argument_handler extends // of 'AND' or 'OR' to create the where clause. $this->query->set_where_group($this->options['date_method'], 'date'); foreach ($this->query_fields as $query_field) { $field = $query_field['field']; $date_handler = $query_field['date_handler']; - $from_field = str_replace($field['table_name'] .'_', $field['table_name'] .'.', $field['fromto'][0]); - $to_field = str_replace($field['table_name'] .'_', $field['table_name'] .'.', $field['fromto'][1]); - if ($this->options['relationship'] !== 'none') { - $from_field = str_replace($field['table_name'] .'_', $this->relationship .'_'. $field['table_name'] .'.', $field['fromto'][0]); - $to_field = str_replace($field['table_name'] .'_', $this->relationship .'_'. $field['table_name'] .'.', $field['fromto'][1]); - $this->table = $this->query_fields[0]['field']['table_name']; - $this->ensure_my_table(); - } + $table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name']; + $from_field = str_replace($field['table_name'] .'_', $table_alias .'.', $field['fromto'][0]); + $to_field = str_replace($field['table_name'] .'_', $table_alias .'.', $field['fromto'][1]); if ($this->granularity != 'week') { $from = $date_handler->sql_where_format($format, $from_field, '<=', date_format($max_date, $format)); $to = $date_handler->sql_where_format($format, $to_field, '>=', date_format($min_date, $format)); } else {