From 058835304480850b29954c2113b594b263114631 Mon Sep 17 00:00:00 2001 From: Boris Cerati Date: Thu, 24 Aug 2017 22:33:27 +0200 Subject: [PATCH] Throw an exception when no links is configured to fetch entities --- src/AbstractMySqlGateway.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/AbstractMySqlGateway.php b/src/AbstractMySqlGateway.php index fd13e27..192d8f6 100644 --- a/src/AbstractMySqlGateway.php +++ b/src/AbstractMySqlGateway.php @@ -58,12 +58,14 @@ public function getLinks($method = self::ALL) { $links = []; - foreach ($this->links as $key => $pool) { - if ($method & $key) { - /** @var Link $link */ - foreach ($pool as $link) { - if ($link->runFilters()) { - $links[] = $link->getLink(); + if (is_array($this->links)) { + foreach ($this->links as $key => $pool) { + if ($method & $key) { + /** @var Link $link */ + foreach ($pool as $link) { + if ($link->runFilters()) { + $links[] = $link->getLink(); + } } } } @@ -80,7 +82,13 @@ public function fetchOne($key): EntityInterface { $class = $this->getEntityClass() ? $this->getEntityClass() : $this->getDefaultEntityClass(); - foreach ($this->getLinks(self::FETCH_ONE) as $link) { + $links = $this->getLinks(self::FETCH_ONE); + + if (!$links) { + throw new MySqlGatewayException('No link found to fetch one entity'); + } + + foreach ($links as $link) { /** @var EntityInterface $entity */ $entity = new $class; @@ -276,6 +284,11 @@ public function fetchAll(ResultSetDescriptorInterface $descriptor): ResultSetInt { $links = $this->getLinks(self::READ); + + if (!$links) { + throw new MySqlGatewayException('No link found to fetch entities'); + } + $query = new Select(new Quoter('`', '`')); $this->hydrateQuery($query, $descriptor);