From 9eb64a6c82864682aa8cdd8c64e2233a4ad57769 Mon Sep 17 00:00:00 2001 From: Andrey Kharanenka Date: Thu, 12 Mar 2020 13:48:09 +0300 Subject: [PATCH] Improved speed of random method in ElementCollection class --- classes/collection/ElementCollection.php | 12 ++++++++++-- updates/version.yaml | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/classes/collection/ElementCollection.php b/classes/collection/ElementCollection.php index aee371a5..ee5462c2 100644 --- a/classes/collection/ElementCollection.php +++ b/classes/collection/ElementCollection.php @@ -394,9 +394,17 @@ public function random($iCount = 1) } $obThis = $this->copy(); - shuffle($obThis->arElementIDList); - return $obThis->take($iCount); + $arElementIDList = $obThis->getIDList(); + $arKeyList = array_rand($arElementIDList, $iCount); + if (!is_array($arKeyList)) { + $arKeyList = [$arKeyList]; + } + + $arKeyList = array_combine($arKeyList, $arKeyList); + $arElementIDList = array_intersect_key($arElementIDList, $arKeyList); + + return $obThis->intersect($arElementIDList)->all(); } /** diff --git a/updates/version.yaml b/updates/version.yaml index b4d3ced1..86ae1238 100644 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -93,4 +93,6 @@ 1.28.0: - 'Added dimension fields to lang files' 1.28.1: - - 'Added passing $this param to get[FieldName]Attribute() method in ElementItem class. It is recommended to use parameter from callback function in the dynamic methods get[FieldName]Attribute in ElementItem class.' \ No newline at end of file + - 'Added passing $this param to get[FieldName]Attribute() method in ElementItem class. It is recommended to use parameter from callback function in the dynamic methods get[FieldName]Attribute in ElementItem class.' +1.29.0: + - 'Improved speed of random method in ElementCollection class.' \ No newline at end of file