diff --git a/core/dbobject.class.php b/core/dbobject.class.php index f701af3b7..d9185af11 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -1859,9 +1859,7 @@ abstract class DBObject implements iDisplay { /** @var \AttributeExternalKey $oAtt */ $sTargetClass = $oAtt->GetTargetClass(); - $oTargetObj = MetaModel::GetObject($sTargetClass, $toCheck, false /*must be found*/, true /*allow all data*/); - if (is_null($oTargetObj)) - { + if (false === MetaModel::IsObjectExistsInDb($sTargetClass, $toCheck)) { return "Target object not found ($sTargetClass::$toCheck)"; } } diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 5aa45a4db..8018d93ae 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -6976,12 +6976,9 @@ abstract class MetaModel if (!utils::IsArchiveMode() && $oObject->IsArchived()) { - if ($bMustBeFound) - { + if ($bMustBeFound) { throw new ArchivedObjectException("The object $sClass::$iKey is archived"); - } - else - { + } else { return null; } } @@ -6989,6 +6986,15 @@ abstract class MetaModel return $oObject; } + public static function IsObjectExistsInDb(string $sClass, int $iKey): bool + { + $oFilter = DBObjectSearch::FromOQL('SELECT '.$sClass.' WHERE id = :id', ['id' => $iKey,]); + $oSet = new DBObjectSet($oFilter); + $iCount = $oSet->Count(); + + return ($iCount > 0); + } + /** * Search for the specified class and id. If the object is archived it will be returned anyway (this is for pre-2.4 * module compatibility, see N.1108)