diff --git a/model/MySQLDatabase.php b/model/MySQLDatabase.php index 9ebfaa614..a5ea5a596 100644 --- a/model/MySQLDatabase.php +++ b/model/MySQLDatabase.php @@ -834,11 +834,12 @@ class MySQLDatabase extends SS_Database { $match['SiteTree'] = $match['File'] = "1 = 1"; } - // Generate initial queries and base table names + // Generate initial DataLists and base table names + $lists = array(); $baseClasses = array('SiteTree' => '', 'File' => ''); foreach($classesToSearch as $class) { - $queries[$class] = singleton($class)->extendedSQL($notMatch . $match[$class] . $extraFilters[$class], ""); - $baseClasses[$class] = reset($queries[$class]->from); + $lists[$class] = DataList::create($class)->where($notMatch . $match[$class] . $extraFilters[$class], ""); + $baseClasses[$class] = '"'.$class.'"'; } // Make column selection lists @@ -847,18 +848,17 @@ class MySQLDatabase extends SS_Database { 'File' => array("ClassName","$baseClasses[File].ID","_utf8'' AS ParentID","Title","_utf8'' AS MenuTitle","_utf8'' AS URLSegment","Content","LastEdited","Created","Filename","Name","$relevance[File] AS Relevance","NULL AS CanViewType"), ); - // Process queries - foreach($classesToSearch as $class) { - // There's no need to do all that joining - $queries[$class]->from = array(str_replace('`','',$baseClasses[$class]) => $baseClasses[$class]); - $queries[$class]->select = $select[$class]; - $queries[$class]->orderby = null; - } - - // Combine queries + // Process and combine queries $querySQLs = array(); $totalCount = 0; - foreach($queries as $query) { + foreach($lists as $class => $list) { + $query = $list->dataQuery()->query(); + + // There's no need to do all that joining + $query->from = array(str_replace(array('"','`'),'',$baseClasses[$class]) => $baseClasses[$class]); + $query->select = $select[$class]; + $query->orderby = null; + $querySQLs[] = $query->sql(); $totalCount += $query->unlimitedRowCount(); } diff --git a/model/Versioned.php b/model/Versioned.php index a4f7698b1..0e6c2107a 100644 --- a/model/Versioned.php +++ b/model/Versioned.php @@ -683,8 +683,11 @@ class Versioned extends DataExtension { // Make sure the table names are not postfixed (e.g. _Live) $oldMode = self::get_reading_mode(); self::reading_stage('Stage'); - - $query = $this->owner->extendedSQL($filter, $sort, $limit, $join, $having); + + $list = DataObject::get(get_class($this->owner), $filter, $sort, $limit, $join); + if($having) $having = $list->having($having); + + $query = $list->dataQuery()->query(); foreach($query->from as $table => $tableJoin) { if(is_string($tableJoin) && $tableJoin[0] == '"') { diff --git a/security/Group.php b/security/Group.php index a82153ba2..e6c00102c 100644 --- a/security/Group.php +++ b/security/Group.php @@ -257,10 +257,7 @@ class Group extends DataObject { // Get the children of *all* the groups identified in the previous chunk. // This minimises the number of SQL queries necessary - $sql = $this->extendedSQL("\"ParentID\" IN ($idList)", ""); - $dbResult = $sql->execute(); - $chunkToAdd = array(); - foreach($dbResult as $item) $chunkToAdd[] = $item; + $chunkToAdd = DataList::create('Group')->where("\"ParentID\" IN ($idList)")->column('ID'); } return $familyIDs;