From 2de6e6425d0ba7f57fd5bd126527971532b87bb6 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Mon, 17 Nov 2014 11:04:58 -0600 Subject: [PATCH] orm: lookup() should return null on failure --- include/class.orm.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/include/class.orm.php b/include/class.orm.php index 664b506f1..7e8cde195 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -299,6 +299,10 @@ class VerySimpleModel { * Parameters: * $criteria - (mixed) primary key for the sought model either as * arguments or key/value array as the function's first argument + * + * Returns: + * (Object<Model>|null) a single instance of the sought model or null if + * no such instance exists. */ static function lookup($criteria) { // Model::lookup(1), where >1< is the pk value @@ -310,7 +314,12 @@ class VerySimpleModel { if ($cached = ModelInstanceManager::checkCache(get_called_class(), $criteria)) return $cached; - return static::objects()->filter($criteria)->one(); + try { + return static::objects()->filter($criteria)->one(); + } + catch (DoesNotExist $e) { + return null; + } } function delete($pk=false) { @@ -593,6 +602,20 @@ class QuerySet implements IteratorAggregate, ArrayAccess { return $list[0]; } + /** + * one + * + * Finds and returns a single model instance based on the criteria in + * this QuerySet instance. + * + * Throws: + * DoesNotExist - if no such model exists with the given criteria + * ObjectNotUnique - if more than one model matches the given criteria + * + * Returns: + * (Object<Model>) a single instance of the sought model is guarenteed. + * If no such model or multiple models exist, an exception is thrown. + */ function one() { $list = $this->all(); if (count($list) == 0) -- GitLab