New Managed_DataObject retrieval: listFind

This will return a proper DB_DataObject instance (as the desired class)
and not an array, or ArrayWrapper.
This commit is contained in:
Mikael Nordfeldth 2013-09-21 16:55:18 +02:00
parent 93e878d7ca
commit 39f21d63af
3 changed files with 93 additions and 7 deletions

View 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 key column. Usually used for the primary key when
* multiple values are desired.
* 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, or null for no hits
* @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. Result is an array.
*
* @param string $keyCol key column name
* @param array $keyVals array of key values
*
* @return array with an get_called_class() object for each $keyVals entry
*
*/
static function listGet($keyCol, array $keyVals)

View 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),

51
lib/noresultexception.php Normal file
View File

@ -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.'));
}
}