From f2f09a233f192bd5ecaac720735891a8414ec5c9 Mon Sep 17 00:00:00 2001 From: Jonathan Baxter Date: Sat, 24 Dec 2016 08:57:05 -0500 Subject: [PATCH] If $ids is an empty array, don't load everything, short circuit and return nothing --- src/Entity_Abstract.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Entity_Abstract.php b/src/Entity_Abstract.php index fb8d7b3..68dbc9e 100644 --- a/src/Entity_Abstract.php +++ b/src/Entity_Abstract.php @@ -247,11 +247,21 @@ public static function __callStatic($name, $arguments) { } public static function load($ids) { + $ids = (array) $ids; + if (empty($ids)) { + return []; + } + $ds = static::getDataSource(); - return $ds->getEntities(get_called_class(), (array) $ids); + return $ds->getEntities(get_called_class(), $ids); } public static function loadProgressive($ids, $eagerFetchProperties = [], $context = null, $contextData = []) { + $ids = (array) $ids; + if (empty($ids)) { + return []; + } + # Eager load in progressive selects $ds = static::getDataSource(); @@ -261,13 +271,13 @@ public static function loadProgressive($ids, $eagerFetchProperties = [], $contex // Add additional contextData if present $where = $contextData + $where; - $entities = call_user_func_array([$ds, 'getDependents'], [get_called_class(), (array) $ids, 'Entity_' . $type, $where, $order, $limit, $count]); + $entities = call_user_func_array([$ds, 'getDependents'], [get_called_class(), $ids, 'Entity_' . $type, $where, $order, $limit, $count]); // Need to make sure this is an array of objects or just null $entities = is_object($entities) ? [$entities] : $entities; } else { - $entities = $ds->getEntities(get_called_class(), (array) $ids); + $entities = $ds->getEntities(get_called_class(), $ids); } // Eager load properties relying on good old recursion to traverse @@ -563,4 +573,4 @@ function($config, $dependent) use ($thisClass) { false ); } -} \ No newline at end of file +}