diff --git a/core/oqlclassnode.class.inc.php b/core/oqlclassnode.class.inc.php index da157f588..3c078ab14 100644 --- a/core/oqlclassnode.class.inc.php +++ b/core/oqlclassnode.class.inc.php @@ -331,4 +331,12 @@ class OQLJoin return $this->sRightField; } + /** + * @return string + */ + public function GetLeftField() + { + return $this->sLeftField; + } + } diff --git a/core/oqlclasstreebuilder.class.inc.php b/core/oqlclasstreebuilder.class.inc.php index 2e25fb1dd..a397ab3db 100644 --- a/core/oqlclasstreebuilder.class.inc.php +++ b/core/oqlclasstreebuilder.class.inc.php @@ -294,13 +294,6 @@ class OQLClassTreeBuilder $oClassExpr = new FieldExpression($sClassAttCode, $this->sClassAlias); $oClassRestriction = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr); $oQBContextExpressions->AddCondition($oClassRestriction); - } elseif (($oKeyAttDef instanceof AttributeExternalKey) && $sKeyClass != $oKeyAttDef->GetTargetClass()) { - $sClassAttCode = 'finalclass'; - $oClassListExpr = ListExpression::FromScalars(MetaModel::EnumChildClasses($sKeyClass, - ENUM_CHILD_CLASSES_ALL)); - $oClassExpr = new FieldExpression($sClassAttCode, $sKeyClassAlias); - $oClassRestriction = new BinaryExpression($oClassExpr, 'IN', $oClassListExpr); - $oQBContextExpressions->AddCondition($oClassRestriction); } // Translate prior to recursing diff --git a/core/oqlclasstreeoptimizer.class.inc.php b/core/oqlclasstreeoptimizer.class.inc.php index 0472863e7..1e12384e9 100644 --- a/core/oqlclasstreeoptimizer.class.inc.php +++ b/core/oqlclasstreeoptimizer.class.inc.php @@ -50,8 +50,15 @@ class OQLClassTreeOptimizer { if ($oJoin->IsOutbound()) { - // The join is not used, remove from tree - $oCurrentClassNode->RemoveJoin($sLeftKey, $index); + // If joined class in not the same class than the external key target class + // then the join cannot be removed because it is used to filter the request + $sJoinedClass = $oJoin->GetOOQLClassNode()->GetNodeClass(); + $sExtKeyAttCode = $oJoin->GetLeftField(); + $oExtKeyAttDef = MetaModel::GetAttributeDef($oCurrentClassNode->GetNodeClass(), $sExtKeyAttCode); + if ($sJoinedClass == $oExtKeyAttDef->GetTargetClass()) { + // The join is not used, remove from tree + $oCurrentClassNode->RemoveJoin($sLeftKey, $index); + } } else { diff --git a/test/core/OQLTest.php b/test/core/OQLTest.php index d419f46b3..54b0293d7 100644 --- a/test/core/OQLTest.php +++ b/test/core/OQLTest.php @@ -441,8 +441,8 @@ class OQLTest extends ItopDataTestCase "SELECT `U` FROM `UserRequest` AS `U` INNER JOIN `lnkContactToTicket` AS `l` ON `U`.`id` = `l`.`ticket_id` - INNER JOIN `Contact` AS `T_Contact` - ON `l`.`contact_id` = `T_Contact`.`id`", + INNER JOIN `Team` AS `T` + ON `l`.`contact_id` = `T`.`id`", ], 'Bug 3660 2' => [ "SELECT UserRequest AS U