]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
New Managed_DataObject retrieval: listFind
authorMikael Nordfeldth <mmn@hethane.se>
Sat, 21 Sep 2013 14:55:18 +0000 (16:55 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Sat, 21 Sep 2013 16:18:03 +0000 (18:18 +0200)
This will return a proper DB_DataObject instance (as the desired class)
and not an array, or ArrayWrapper.

classes/Managed_DataObject.php
classes/Memcached_DataObject.php
lib/noresultexception.php [new file with mode: 0644]

index 0e328b151416e20b4a29268f8be3be419d31b62e..80210dc8a32179ca3fdc98f9a346f229c086ce66 100644 (file)
@@ -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)
index 288457c0f1a7933e8e33e968777be7d50538021f..7689ea979bd14976da7efa8e49902442901c399c 100644 (file)
@@ -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 (file)
index 0000000..d847852
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * class for an exception when a database lookup returns no results
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Exception
+ * @package   StatusNet
+ * @author    Mikael Nordfeldth <mmn@hethane.se>
+ * @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 <mmn@hethane.se>
+ * @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.'));
+    }
+}