From 0d683dd8efe076d62036ccf9cea370d3caf76124 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Sat, 29 Oct 2011 17:11:27 +1300 Subject: [PATCH] MINOR: Removed use of deprecated join() in favour of innerJoin() and leftJoin(). --- forms/TableListField.php | 4 ++-- model/DataList.php | 1 + model/DataObject.php | 6 ++++-- security/Group.php | 10 ++++++++-- security/Permission.php | 22 ++++++++-------------- tests/model/DataQueryTest.php | 10 +++++++--- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/forms/TableListField.php b/forms/TableListField.php index bf95343d8..278e8a9ad 100644 --- a/forms/TableListField.php +++ b/forms/TableListField.php @@ -240,8 +240,8 @@ class TableListField extends FormField { $this->dataList = $sourceClass; } else { - $this->dataList = DataObject::get($sourceClass)->where($sourceFilter) - ->sort($sourceSort)->join($sourceJoin); + $this->dataList = DataObject::get($sourceClass)->where($sourceFilter)->sort($sourceSort); + if($sourceJoin) $this->dataList = $this->dataList->join($sourceJoin); // Grab it from the form relation, if available. $this->getDataListFromForm = true; } diff --git a/model/DataList.php b/model/DataList.php index 42cf14cbe..0c1715dfe 100644 --- a/model/DataList.php +++ b/model/DataList.php @@ -97,6 +97,7 @@ class DataList extends ViewableData implements SS_List { * Add an join clause to this data list's query. */ public function join($join) { + Deprecation::notice('3.0', 'Use innerJoin() or leftJoin() instead.'); $this->dataQuery->join($join); return $this; } diff --git a/model/DataObject.php b/model/DataObject.php index 49ffcf000..30e2d51d2 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -1336,7 +1336,8 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity if($this->model) $result->setModel($this->model); if($this->ID) $result->setForeignID($this->ID); - $result = $result->where($filter)->limit($limit)->sort($sort)->join($join); + $result = $result->where($filter)->limit($limit)->sort($sort); + if($join) $result = $result->join($join); return $result; } @@ -2496,7 +2497,8 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity // Todo: Determine if we can deprecate for 3.0.0 and use DI or something instead // Todo: Make the $containerClass method redundant if($containerClass != "DataList") user_error("The DataObject::get() \$containerClass argument has been deprecated", E_USER_NOTICE); - $result = DataList::create($callerClass)->where($filter)->sort($sort)->join($join)->limit($limit); + $result = DataList::create($callerClass)->where($filter)->sort($sort)->limit($limit); + if($join) $result = $result->join($join); $result->setModel(DataModel::inst()); return $result; } diff --git a/security/Group.php b/security/Group.php index 994f35c57..a82153ba2 100644 --- a/security/Group.php +++ b/security/Group.php @@ -208,10 +208,16 @@ class Group extends DataObject { public function Members($filter = "", $sort = "", $join = "", $limit = "") { // Get a DataList of the relevant groups $groups = DataList::create("Group")->byIDs($this->collateFamilyIDs()); + + if($sort || $join || $limit) { + Deprecation::notice('3.0', "The sort, join, and limit arguments are deprcated, use sort(), join() and limit() on the resulting DataList instead."); + } // Call the relation method on the DataList to get the members from all the groups - return $groups->relation('DirectMembers') - ->where($filter)->sort($sort)->join($join)->limit($limit); + $result = $groups->relation('DirectMembers')->where($filter)->sort($sort)->limit($limit); + if($join) $result = $result->join($join); + + return $result; } /** diff --git a/security/Permission.php b/security/Permission.php index cefd22413..30c226a67 100644 --- a/security/Permission.php +++ b/security/Permission.php @@ -391,13 +391,9 @@ class Permission extends DataObject { if(!count($groupIDs)) return new ArrayList(); - $members = DataObject::get( - Object::getCustomClass('Member'), - $_filter = "\"Group\".\"ID\" IN (" . implode(",",$groupIDs) . ")", - $_sort = "", - $_join = "LEFT JOIN \"Group_Members\" ON \"Member\".\"ID\" = \"Group_Members\".\"MemberID\" " . - "LEFT JOIN \"Group\" ON \"Group_Members\".\"GroupID\" = \"Group\".\"ID\" " - ); + $members = DataObject::get('Member')->where("\"Group\".\"ID\" IN (" . implode(",",$groupIDs) . ")") + ->leftJoin("Group_Members", "\"Member\".\"ID\" = \"Group_Members\".\"MemberID\"") + ->leftJoin("Group", "\"Group_Members\".\"GroupID\" = \"Group\".\"ID\""); return $members; } @@ -414,13 +410,11 @@ class Permission extends DataObject { $SQL_codes = join("','", $SQLa_codes); // Via Roles are groups that have the permission via a role - return DataObject::get('Group', - "\"PermissionRoleCode\".\"Code\" IN ('$SQL_codes') OR \"Permission\".\"Code\" IN ('$SQL_codes')", - "", - "LEFT JOIN \"Permission\" ON \"Permission\".\"GroupID\" = \"Group\".\"ID\" - LEFT JOIN \"Group_Roles\" ON \"Group_Roles\".\"GroupID\" = \"Group\".\"ID\" - LEFT JOIN \"PermissionRole\" ON \"Group_Roles\".\"PermissionRoleID\" = \"PermissionRole\".\"ID\" - LEFT JOIN \"PermissionRoleCode\" ON \"PermissionRoleCode\".\"RoleID\" = \"PermissionRole\".\"ID\""); + return DataObject::get('Group')->where("\"PermissionRoleCode\".\"Code\" IN ('$SQL_codes') OR \"Permission\".\"Code\" IN ('$SQL_codes')") + ->leftJoin('Permission', "\"Permission\".\"GroupID\" = \"Group\".\"ID\"") + ->leftJoin('Group_Roles', "\"Group_Roles\".\"GroupID\" = \"Group\".\"ID\"") + ->leftJoin('PermissionRole', "\"Group_Roles\".\"PermissionRoleID\" = \"PermissionRole\".\"ID\"") + ->leftJoin('PermissionRoleCode', "\"PermissionRoleCode\".\"RoleID\" = \"PermissionRole\".\"ID\""); } diff --git a/tests/model/DataQueryTest.php b/tests/model/DataQueryTest.php index 972377a88..a4ffa4ccb 100644 --- a/tests/model/DataQueryTest.php +++ b/tests/model/DataQueryTest.php @@ -2,12 +2,16 @@ class DataQueryTest extends SapphireTest { /** - * Test the join() method of the DataQuery object + * Test the leftJoin() and innerJoin method of the DataQuery object */ - function testJoin() { + function testJoins() { $dq = new DataQuery('Member'); - $dq->join("INNER JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\""); + $dq->innerJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\""); $this->assertContains("INNER JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"", $dq->sql()); + + $dq = new DataQuery('Member'); + $dq->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\""); + $this->assertContains("LEFT JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"", $dq->sql()); } }