Don't use DB_DataObject::factory (statically at least)

Not all instances of this has been fixed, but at least the ones
in the base class of Memcached_DataObject.

Avatar fix (in classes/Profile.php) requires a pkeyGet function
in the Avatar class (or as in this tree, the parent class of
Managed_DataObject)
This commit is contained in:
Mikael Nordfeldth 2013-08-19 11:40:35 +02:00
parent 97ce71e55d
commit 0785cc2469
2 changed files with 24 additions and 28 deletions

View File

@ -32,6 +32,9 @@ class Memcached_DataObject extends Safe_DataObject
*/ */
static function getClassKV($cls, $k, $v=null) static function getClassKV($cls, $k, $v=null)
{ {
if (!is_a($cls, __CLASS__, true)) {
throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
}
if (is_null($v)) { if (is_null($v)) {
$v = $k; $v = $k;
$keys = self::pkeyCols($cls); $keys = self::pkeyCols($cls);
@ -41,13 +44,9 @@ class Memcached_DataObject extends Safe_DataObject
} }
$k = $keys[0]; $k = $keys[0];
} }
$i = Memcached_DataObject::getcached($cls, $k, $v); $i = self::getcached($cls, $k, $v);
if ($i === false) { // false == cache miss if ($i === false) { // false == cache miss
$i = DB_DataObject::factory($cls); $i = new $cls;
if (empty($i)) {
$i = false;
return $i;
}
$result = $i->get($k, $v); $result = $i->get($k, $v);
if ($result) { if ($result) {
// Hit! // Hit!
@ -106,6 +105,9 @@ class Memcached_DataObject extends Safe_DataObject
*/ */
static function pivotGet($cls, $keyCol, $keyVals, $otherCols = array()) static function pivotGet($cls, $keyCol, $keyVals, $otherCols = array())
{ {
if (!is_a($cls, __CLASS__, true)) {
throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
}
if (is_array($keyCol)) { if (is_array($keyCol)) {
foreach ($keyVals as $keyVal) { foreach ($keyVals as $keyVal) {
$result[implode(',', $keyVal)] = null; $result[implode(',', $keyVal)] = null;
@ -140,11 +142,7 @@ class Memcached_DataObject extends Safe_DataObject
} }
if (count($toFetch) > 0) { if (count($toFetch) > 0) {
$i = DB_DataObject::factory($cls); $i = new $cls;
if (empty($i)) {
// TRANS: Exception thrown when a program code class (%s) cannot be instantiated.
throw new Exception(sprintf(_('Cannot instantiate class %s.'),$cls));
}
foreach ($otherCols as $otherKeyCol => $otherKeyVal) { foreach ($otherCols as $otherKeyCol => $otherKeyVal) {
$i->$otherKeyCol = $otherKeyVal; $i->$otherKeyCol = $otherKeyVal;
} }
@ -248,11 +246,10 @@ class Memcached_DataObject extends Safe_DataObject
static function pkeyCols($cls) static function pkeyCols($cls)
{ {
$i = DB_DataObject::factory($cls); if (!is_a($cls, __CLASS__, true)) {
if (empty($i)) { throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
// TRANS: Exception thrown when a program code class (%s) cannot be instantiated.
throw new Exception(sprintf(_('Cannot instantiate class %s.'),$cls));
} }
$i = new $cls;
$types = $i->keyTypes(); $types = $i->keyTypes();
ksort($types); ksort($types);
@ -269,6 +266,9 @@ class Memcached_DataObject extends Safe_DataObject
static function listGetClass($cls, $keyCol, $keyVals) static function listGetClass($cls, $keyCol, $keyVals)
{ {
if (!is_a($cls, __CLASS__, true)) {
throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
}
$pkeyMap = array_fill_keys($keyVals, array()); $pkeyMap = array_fill_keys($keyVals, array());
$result = array_fill_keys($keyVals, array()); $result = array_fill_keys($keyVals, array());
@ -305,11 +305,7 @@ class Memcached_DataObject extends Safe_DataObject
} }
if (count($toFetch) > 0) { if (count($toFetch) > 0) {
$i = DB_DataObject::factory($cls); $i = new $cls;
if (empty($i)) {
// TRANS: Exception thrown when a program code class (%s) cannot be instantiated.
throw new Exception(sprintf(_('Cannot instantiate class %s.'),$cls));
}
$i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol)); $i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol));
if ($i->find()) { if ($i->find()) {
sprintf(__CLASS__ . "() got {$i->N} results for class $cls key $keyCol"); sprintf(__CLASS__ . "() got {$i->N} results for class $cls key $keyCol");
@ -354,14 +350,14 @@ class Memcached_DataObject extends Safe_DataObject
*/ */
static function pkeyGetClass($cls, $kv) static function pkeyGetClass($cls, $kv)
{ {
if (!is_a($cls, __CLASS__, true)) {
throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
}
$i = Memcached_DataObject::multicache($cls, $kv); $i = Memcached_DataObject::multicache($cls, $kv);
if ($i !== false) { // false == cache miss if ($i !== false) { // false == cache miss
return $i; return $i;
} else { } else {
$i = DB_DataObject::factory($cls); $i = new $cls;
if (empty($i) || PEAR::isError($i)) {
return false;
}
foreach ($kv as $k => $v) { foreach ($kv as $k => $v) {
if (is_null($v)) { if (is_null($v)) {
// XXX: possible SQL injection...? Don't // XXX: possible SQL injection...? Don't

View File

@ -155,10 +155,10 @@ class Profile extends Managed_DataObject
function getOriginalAvatar() function getOriginalAvatar()
{ {
$avatar = DB_DataObject::factory('avatar'); $pkey = array('profile_id' => $this->id,
$avatar->profile_id = $this->id; 'original' => true);
$avatar->original = true; $avatar = Avatar::pkeyGet($pkey);
if ($avatar->find(true)) { if (!empty($avatar)) {
return $avatar; return $avatar;
} else { } else {
return null; return null;