From 466a8b771fd6d2b8a0318f4454ef7d585d9d2106 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 16 Mar 2022 08:39:22 +0100 Subject: [PATCH] Add QBMapper::findById As of Nextcloud 24 it is a requirement that all tables have a primary key. This allows us to make an assumption about what a findById looks like. This is a kind-of breaking change due to the *fragile base class* problem. Therefore we need to give app devs a heads-up before this is added. E.g. by telling them to rename their existing findById methods if they want to support wider ranges of Nextcloud or drop theirs in favor of the new base class method. Signed-off-by: Christoph Wurst --- lib/public/AppFramework/Db/QBMapper.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/public/AppFramework/Db/QBMapper.php b/lib/public/AppFramework/Db/QBMapper.php index 5124650bc193a..f7fe9e61ddabd 100644 --- a/lib/public/AppFramework/Db/QBMapper.php +++ b/lib/public/AppFramework/Db/QBMapper.php @@ -83,6 +83,26 @@ public function getTableName(): string { return $this->tableName; } + /** + * Locate one row by its primary key and throw when nothing is found + * + * @param IQueryBuilder $query + * @return Entity the result + * @psalm-return T the result + * @throws DoesNotExistException if the item does not exist + * + * @since 25.0.0 + */ + public function findById(int $id): Entity { + $qb = $this->db->getQueryBuilder(); + $select = $qb->select('*') + ->from($this->getTableName()) + ->where( + $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT) + ); + return $this->findEntity($select); + } + /** * Deletes an entity from the table