diff --git a/models/Table/CollectionTree.php b/models/Table/CollectionTree.php index f52624f..e7ec0db 100644 --- a/models/Table/CollectionTree.php +++ b/models/Table/CollectionTree.php @@ -21,6 +21,14 @@ class Table_CollectionTree extends Omeka_Db_Table */ protected $_collections; + /** + * Cache of children of a collection. + * + * It's an associative array where keys are collection ids and values are + * arrays of children ids. + */ + protected $_collectionsChildren; + /** * Cache of variables needed for some use. * @@ -271,12 +279,14 @@ public function getCollection($collectionId) public function getChildCollections($collectionId) { $childCollections = array(); - $collections = $this->_getCollections(); - foreach ($collections as $collection) { - if ($collectionId == $collection['parent_collection_id']) { - $childCollections[$collection['id']] = $collection; + $collectionsChildren = $this->_getCollectionsChildren(); + + if (isset($collectionsChildren[$collectionId])) { + foreach ($collectionsChildren[$collectionId] as $childId) { + $childCollections[$childId] = $this->getCollection($childId); } } + return $childCollections; } @@ -365,6 +375,26 @@ protected function _getCollections() return $this->_collections; } + /** + * Cache collections children data in an associative array. + */ + protected function _getCollectionsChildren() + { + if (is_null($this->_collectionsChildren)) { + $collections = $this->_getCollections(); + + $this->_collectionsChildren = array(); + foreach ($collections as $id => $collection) { + if ($collection['parent_collection_id']) { + $parent_collection_id = $collection['parent_collection_id']; + $this->_collectionsChildren[$parent_collection_id][] = $id; + } + } + } + + return $this->_collectionsChildren; + } + /** * Reset the cache property. */