diff --git a/model/DataQuery.php b/model/DataQuery.php
index 361515820..65460bd95 100644
--- a/model/DataQuery.php
+++ b/model/DataQuery.php
@@ -139,6 +139,8 @@ class DataQuery {
/**
* Ensure that the query is ready to execute.
+ *
+ * @return SQLQuery
*/
public function getFinalisedQuery($queriedColumns = null) {
if(!$queriedColumns) $queriedColumns = $this->queriedColumns;
@@ -277,16 +279,14 @@ class DataQuery {
if(isset($databaseFields[$parts[0]])) {
$qualCol = "\"$baseClass\".\"{$parts[0]}\"";
-
- // remove original sort
- unset($orderby[$k]);
-
- // add new columns sort
- $orderby[$qualCol] = $dir;
-
} else {
$qualCol = "\"$parts[0]\"";
}
+
+ // remove original sort
+ unset($orderby[$k]);
+ // add new columns sort
+ $orderby[$qualCol] = $dir;
// To-do: Remove this if block once SQLQuery::$select has been refactored to store getSelect()
// format internally; then this check can be part of selectField()
@@ -419,7 +419,7 @@ class DataQuery {
}
/**
- * Set the HAVING clause of this query
+ * Set the HAVING clause of this query.
*
* @param String $having Escaped SQL statement
*/
@@ -458,10 +458,10 @@ class DataQuery {
*
*
* // the entire predicate as a single string
- * $query->where("Column = 'Value'");
+ * $query->where("\"Column\" = 'Value'");
*
* // multiple predicates as an array
- * $query->where(array("Column = 'Value'", "Column != 'Value'"));
+ * $query->where(array("\"Column\" = 'Value'", "\"Column\" != 'Value'"));
*
*
* @param string|array $where Predicate(s) to set, as escaped SQL statements.
@@ -476,7 +476,7 @@ class DataQuery {
/**
* Set a WHERE with OR.
*
- * @example $dataQuery->whereAny(array("Monkey = 'Chimp'", "Color = 'Brown'"));
+ * @example $dataQuery->whereAny(array("\"Monkey\" = 'Chimp'", "\"Color\" = 'Brown'"));
* @see where()
*
* @param array $filter Escaped SQL statement.
@@ -778,10 +778,10 @@ class DataQuery_SubGroup extends DataQuery {
*
*
* // the entire predicate as a single string
- * $query->where("Column = 'Value'");
+ * $query->where("\"Column\" = 'Value'");
*
* // multiple predicates as an array
- * $query->where(array("Column = 'Value'", "Column != 'Value'"));
+ * $query->where(array("\"Column\" = 'Value'", "\"Column\" != 'Value'"));
*
*
* @param string|array $where Predicate(s) to set, as escaped SQL statements.
@@ -796,7 +796,7 @@ class DataQuery_SubGroup extends DataQuery {
/**
* Set a WHERE with OR.
*
- * @example $dataQuery->whereAny(array("Monkey = 'Chimp'", "Color = 'Brown'"));
+ * @example $dataQuery->whereAny(array("\"Monkey\" = 'Chimp'", "\"Color\" = 'Brown'"));
* @see where()
*
* @param array $filter Escaped SQL statement.
diff --git a/model/Database.php b/model/Database.php
index 7770f03d2..ea73d5503 100644
--- a/model/Database.php
+++ b/model/Database.php
@@ -750,6 +750,8 @@ abstract class SS_Database {
/**
* Returns the SELECT clauses ready for inserting into a query.
+ * Caution: Expects correctly quoted and escaped SQL fragments.
+ *
* @param array $select Select columns
* @param boolean $distinct Distinct select?
* @return string
@@ -770,6 +772,8 @@ abstract class SS_Database {
/**
* Return the FROM clause ready for inserting into a query.
+ * Caution: Expects correctly quoted and escaped SQL fragments.
+ *
* @return string
*/
public function sqlFromToString($from) {
@@ -778,6 +782,8 @@ abstract class SS_Database {
/**
* Returns the WHERE clauses ready for inserting into a query.
+ * Caution: Expects correctly quoted and escaped SQL fragments.
+ *
* @return string
*/
public function sqlWhereToString($where, $connective) {
@@ -786,6 +792,8 @@ abstract class SS_Database {
/**
* Returns the ORDER BY clauses ready for inserting into a query.
+ * Caution: Expects correctly quoted and escaped SQL fragments.
+ *
* @return string
*/
public function sqlOrderByToString($orderby) {
@@ -800,6 +808,8 @@ abstract class SS_Database {
/**
* Returns the GROUP BY clauses ready for inserting into a query.
+ * Caution: Expects correctly quoted and escaped SQL fragments.
+ *
* @return string
*/
public function sqlGroupByToString($groupby) {
@@ -808,6 +818,8 @@ abstract class SS_Database {
/**
* Returns the HAVING clauses ready for inserting into a query.
+ * Caution: Expects correctly quoted and escaped SQL fragments.
+ *
* @return string
*/
public function sqlHavingToString($having) {
@@ -816,6 +828,8 @@ abstract class SS_Database {
/**
* Return the LIMIT clause ready for inserting into a query.
+ * Caution: Expects correctly quoted and escaped SQL fragments.
+ *
* @return string
*/
public function sqlLimitToString($limit) {
@@ -847,6 +861,8 @@ abstract class SS_Database {
/**
* Convert a SQLQuery object into a SQL statement
+ * Caution: Expects correctly quoted and escaped SQL fragments.
+ *
* @param $query SQLQuery
*/
public function sqlQueryToString(SQLQuery $query) {
diff --git a/model/ManyManyList.php b/model/ManyManyList.php
index 52aa3d275..defb15fa9 100644
--- a/model/ManyManyList.php
+++ b/model/ManyManyList.php
@@ -180,6 +180,7 @@ class ManyManyList extends RelationList {
$from = $query->getFrom();
unset($from[$this->joinTable]);
$query->setFrom($from);
+ $query->setDistinct(false);
// Use a sub-query as SQLite does not support setting delete targets in
// joined queries.
diff --git a/model/SQLQuery.php b/model/SQLQuery.php
index e23e191c1..ec84d0e7c 100644
--- a/model/SQLQuery.php
+++ b/model/SQLQuery.php
@@ -1071,6 +1071,7 @@ class SQLQuery {
$clone->setOrderBy($this->orderby);
$clone->setGroupBy($this->groupby);
if($alias) {
+ $clone->setSelect(array());
$clone->selectField($column, $alias);
} else {
$clone->setSelect($column);
diff --git a/tests/forms/gridfield/GridFieldDetailFormTest.php b/tests/forms/gridfield/GridFieldDetailFormTest.php
index ca3e87dbc..055badcda 100644
--- a/tests/forms/gridfield/GridFieldDetailFormTest.php
+++ b/tests/forms/gridfield/GridFieldDetailFormTest.php
@@ -242,7 +242,7 @@ class GridFieldDetailFormTest_Person extends DataObject implements TestOnly {
)
);
- private static $default_sort = 'FirstName';
+ private static $default_sort = '"FirstName"';
public function getCMSFields() {
$fields = parent::getCMSFields();
@@ -266,7 +266,7 @@ class GridFieldDetailFormTest_PeopleGroup extends DataObject implements TestOnly
'People' => 'GridFieldDetailFormTest_Person'
);
- private static $default_sort = 'Name';
+ private static $default_sort = '"Name"';
public function getCMSFields() {
$fields = parent::getCMSFields();
@@ -290,7 +290,7 @@ class GridFieldDetailFormTest_Category extends DataObject implements TestOnly {
'People' => 'GridFieldDetailFormTest_Person'
);
- private static $default_sort = 'Name';
+ private static $default_sort = '"Name"';
public function getCMSFields() {
$fields = parent::getCMSFields();
diff --git a/tests/model/DataObjectTest.php b/tests/model/DataObjectTest.php
index c651e64e9..aa8a45782 100644
--- a/tests/model/DataObjectTest.php
+++ b/tests/model/DataObjectTest.php
@@ -1161,7 +1161,7 @@ class DataObjectTest_Team extends DataObject implements TestOnly {
)
);
- private static $default_sort = "Title";
+ private static $default_sort = '"Title"';
public function MyTitle() {
return 'Team ' . $this->Title;
diff --git a/tests/model/SQLQueryTest.php b/tests/model/SQLQueryTest.php
index 45a1f2b8c..ac5058627 100755
--- a/tests/model/SQLQueryTest.php
+++ b/tests/model/SQLQueryTest.php
@@ -374,7 +374,7 @@ class SQLQueryTest extends SapphireTest {
public function testAggregate() {
$query = new SQLQuery();
$query->setFrom('"SQLQueryTest_DO"');
- $query->setGroupBy("Common");
+ $query->setGroupBy('"Common"');
$queryClone = $query->aggregate('COUNT(*)', 'cnt');
$result = $queryClone->execute();