Skip to content

Commit 9906995

Browse files
committed
use table alias/name when building date query
use the received table name & table alias arguments in the `get_sql` function to set those as properties on the Date class. Then, when building the SQL query, use the alias or name (only if available) to prepend to the column name in the query. This prevents and fixes the issue described in awesomemotive#9699
1 parent 715373d commit 9906995

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

includes/database/engine/class-date.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ class Date extends Base {
137137
'AND'
138138
);
139139

140+
141+
/**
142+
* @since 3.2.6
143+
* @var string|null Table name
144+
*/
145+
public $table_name = null;
146+
147+
/**
148+
* @since 3.2.6
149+
* @var string|null Table alias
150+
*/
151+
public $table_alias = null;
152+
140153
/**
141154
* Constructor.
142155
*
@@ -362,6 +375,12 @@ public function get_column( $query = array() ) {
362375
? esc_sql( $this->validate_column( $query['column'] ) )
363376
: $this->column;
364377

378+
if (!empty($this->table_alias)) {
379+
$retval = $this->table_alias . '.' . $retval;
380+
} elseif (!empty($this->table_name)) {
381+
$retval = $this->table_name . '.' . $retval;
382+
}
383+
365384
return $retval;
366385
}
367386

@@ -602,11 +621,17 @@ public function validate_column( $column = '' ) {
602621
* Generate WHERE clause to be appended to a main query.
603622
*
604623
* @since 1.0.0
624+
* @param string $table_name Optional. Table name. Default null.
625+
* @param string $table_alias Optional. Table alias. Default null.
605626
*
606627
* @return string MySQL WHERE clauses.
607628
*/
608629
public function get_sql() {
609630
$sql = $this->get_sql_clauses();
631+
public function get_sql( $table_name = null, $table_alias = null ) {
632+
$this->table_name = $this->sanitize_table_name( $table_name );
633+
$this->table_alias = $this->sanitize_table_name( $table_alias );
634+
$sql = $this->get_sql_clauses();
610635

611636
/**
612637
* Filters the date query clauses.

0 commit comments

Comments
 (0)