From 39f21d63af22430949eab2fabbc4a922f0e1cf73 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sat, 21 Sep 2013 16:55:18 +0200 Subject: [PATCH] New Managed_DataObject retrieval: listFind This will return a proper DB_DataObject instance (as the desired class) and not an array, or ArrayWrapper. --- classes/Managed_DataObject.php | 22 ++++++++++++-- classes/Memcached_DataObject.php | 25 +++++++++++++--- lib/noresultexception.php | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 lib/noresultexception.php diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php index 0e328b1514..80210dc8a3 100644 --- a/classes/Managed_DataObject.php +++ b/classes/Managed_DataObject.php @@ -93,13 +93,31 @@ abstract class Managed_DataObject extends Memcached_DataObject * Get a multi-instance object * * This is a utility method to get multiple instances with a given set of + * values for a specific column. + * + * @param string $keyCol key column name + * @param array $keyVals array of key values + * + * @return get_called_class() object with multiple instances if found, + * Exception is thrown when no entries are found. + * + */ + static function listFind($keyCol, array $keyVals) + { + return parent::listFindClass(get_called_class(), $keyCol, $keyVals); + } + + /** + * Get a multi-instance object in an array + * + * This is a utility method to get multiple instances with a given set of * values for a specific key column. Usually used for the primary key when - * multiple values are desired. + * multiple values are desired. Result is an array. * * @param string $keyCol key column name * @param array $keyVals array of key values * - * @return get_called_class() object with multiple instances if found, or null for no hits + * @return array with an get_called_class() object for each $keyVals entry * */ static function listGet($keyCol, array $keyVals) diff --git a/classes/Memcached_DataObject.php b/classes/Memcached_DataObject.php index 288457c0f1..7689ea979b 100644 --- a/classes/Memcached_DataObject.php +++ b/classes/Memcached_DataObject.php @@ -264,6 +264,22 @@ class Memcached_DataObject extends Safe_DataObject return $pkey; } + static function listFindClass($cls, $keyCol, array $keyVals) + { + if (!is_a($cls, __CLASS__, true)) { + throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class'); + } + + $i = new $cls; + $i->whereAddIn($keyCol, $keyVals, $i->columnType($keyCol)); + if (!$i->find()) { + throw new NoResultException($i); + } + + sprintf(__CLASS__ . "() got {$i->N} results for class $cls key $keyCol"); + return $i; + } + static function listGetClass($cls, $keyCol, array $keyVals) { if (!is_a($cls, __CLASS__, true)) { @@ -305,10 +321,9 @@ class Memcached_DataObject extends Safe_DataObject } if (count($toFetch) > 0) { - $i = new $cls; - $i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol)); - if ($i->find()) { - sprintf(__CLASS__ . "() got {$i->N} results for class $cls key $keyCol"); + try { + $i = self::listFindClass($cls, $keyCol, $toFetch); + while ($i->fetch()) { $copy = clone($i); $copy->encache(); @@ -319,6 +334,8 @@ class Memcached_DataObject extends Safe_DataObject } $pkeyMap[$i->$keyCol][] = $pkeyVal; } + } catch (NoResultException $e) { + // no results foudn for our keyVals, so we leave them as empty arrays } foreach ($toFetch as $keyVal) { self::cacheSet(sprintf("%s:list-ids:%s:%s", strtolower($cls), $keyCol, $keyVal), diff --git a/lib/noresultexception.php b/lib/noresultexception.php new file mode 100644 index 0000000000..d847852282 --- /dev/null +++ b/lib/noresultexception.php @@ -0,0 +1,51 @@ +. + * + * @category Exception + * @package StatusNet + * @author Mikael Nordfeldth + * @copyright 2013 Free Software Foundation, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ + +if (!defined('GNUSOCIAL')) { + exit(1); +} + +/** + * Class for an exception when a local user is not found by certain criteria + * + * @category Exception + * @package StatusNet + * @author Mikael Nordfeldth + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ + +class NoResultException extends ServerException +{ + public function __construct(DB_DataObject $obj) + { + // We could log an entry here with the search parameters + parent::__construct(_('No result found on lookup.')); + } +} -- 2.39.2