+
+ /**
+ * Get multiple items from the database by key
+ *
+ * @param string $cls Class to fetch
+ * @param string $keyCol name of column for key
+ * @param array $keyVals key values to fetch
+ * @param boolean $skipNulls return only non-null results?
+ *
+ * @return array Array of objects, in order
+ */
+ function multiGet($cls, $keyCol, $keyVals, $skipNulls=true)
+ {
+ $result = array_fill_keys($keyVals, null);
+
+ $toFetch = array();
+
+ foreach ($keyVals as $keyVal) {
+ $i = self::getcached($cls, $keyCol, $keyVal);
+ if ($i !== false) {
+ $result[$keyVal] = $i;
+ } else if (!empty($keyVal)) {
+ $toFetch[] = $keyVal;
+ }
+ }
+
+ if (count($toFetch) > 0) {
+ $i = DB_DataObject::factory($cls);
+ if (empty($i)) {
+ throw new Exception(_('Cannot instantiate class ' . $cls));
+ }
+ $i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol));
+ if ($i->find()) {
+ while ($i->fetch()) {
+ $copy = clone($i);
+ $copy->encache();
+ $result[$i->$keyCol] = $copy;
+ }
+ }
+
+ // Save state of DB misses
+
+ foreach ($toFetch as $keyVal) {
+ if (empty($result[$keyVal])) {
+ // save the fact that no such row exists
+ $c = self::memcache();
+ if (!empty($c)) {
+ $ck = self::cachekey($cls, $keyCol, $keyVal);
+ $c->set($ck, null);
+ }
+ }
+ }
+ }
+
+ $values = array_values($result);
+
+ if ($skipNulls) {
+ $tmp = array();
+ foreach ($values as $value) {
+ if (!empty($value)) {
+ $tmp[] = $value;
+ }
+ }
+ $values = $tmp;
+ }
+
+ return new ArrayWrapper($values);
+ }