From c71c3f194e6ac77a9ce43320d6781aa2ead2d6ea Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Mon, 10 Feb 2020 21:03:17 +0100 Subject: [PATCH 1/3] Fix missing members when getting "latest" organ This fixes a bug I introduced when wanting to select the latest known instance of an organ. --- module/Decision/src/Decision/Mapper/Organ.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/module/Decision/src/Decision/Mapper/Organ.php b/module/Decision/src/Decision/Mapper/Organ.php index 3ab2ab8d14..071d72a563 100644 --- a/module/Decision/src/Decision/Mapper/Organ.php +++ b/module/Decision/src/Decision/Mapper/Organ.php @@ -122,18 +122,25 @@ public function findByAbbr($abbr, $type = null, $latest = false) $qb->select('o, om, m') ->leftJoin('o.members', 'om') ->leftJoin('om.member', 'm') - ->where('o.abbr = :abbr'); + ->where('o.abbr = :abbr') + ->setParameter('abbr', $abbr); if (!is_null($type)) { $qb->andWhere('o.type = :type') ->setParameter('type', $type); } if ($latest) { - $qb->orderBy('o.foundationDate', 'DESC') - ->setMaxResults(1); + $qb->orderBy('o.foundationDate', 'DESC'); + $queryResult = $qb->getQuery()->getResult(); + + if (count($queryResult) == 0) { + // the query did not return any records + throw new \Doctrine\ORM\NoResultException('no organ found'); + } + + // the query did at least return 1 record, use that record + return $queryResult[0]; } - $qb->setParameter('abbr', $abbr); - return $qb->getQuery()->getSingleResult(); } From 460dec50eb0bf3dad6bfab2b7e8720afb6cffedc Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Mon, 10 Feb 2020 21:11:55 +0100 Subject: [PATCH 2/3] Make PSR-2 compliant --- module/Decision/src/Decision/Mapper/Organ.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/module/Decision/src/Decision/Mapper/Organ.php b/module/Decision/src/Decision/Mapper/Organ.php index 071d72a563..d4b73df531 100644 --- a/module/Decision/src/Decision/Mapper/Organ.php +++ b/module/Decision/src/Decision/Mapper/Organ.php @@ -106,7 +106,7 @@ public function find($id) * * It is possible that multiple organs with the same abbreviation exist, * for example, through the reinstatement of an previously abrogated organ. - * To retrieve the latest occurence of such an organ use `$latest`. + * To retrieve the latest occurence of such an organ use `$latest`. * * @param string $abbr * @param string $type @@ -131,12 +131,12 @@ public function findByAbbr($abbr, $type = null, $latest = false) if ($latest) { $qb->orderBy('o.foundationDate', 'DESC'); $queryResult = $qb->getQuery()->getResult(); - + if (count($queryResult) == 0) { // the query did not return any records throw new \Doctrine\ORM\NoResultException('no organ found'); } - + // the query did at least return 1 record, use that record return $queryResult[0]; } From 360e8f97f3e3ff3efbf93f1688603aeb613982fd Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Mon, 10 Feb 2020 21:17:11 +0100 Subject: [PATCH 3/3] Change wording in comment --- module/Decision/src/Decision/Mapper/Organ.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/Decision/src/Decision/Mapper/Organ.php b/module/Decision/src/Decision/Mapper/Organ.php index d4b73df531..8243724605 100644 --- a/module/Decision/src/Decision/Mapper/Organ.php +++ b/module/Decision/src/Decision/Mapper/Organ.php @@ -137,7 +137,7 @@ public function findByAbbr($abbr, $type = null, $latest = false) throw new \Doctrine\ORM\NoResultException('no organ found'); } - // the query did at least return 1 record, use that record + // the query returned at least 1 record, use first (= latest) record return $queryResult[0]; }