Commit db847fa05f5494b435dab20b13d1c10d144fcac0

Authored by nbm
1 parent 0d507059

Add getBy to KTEntityUtil, which gets a single object based on an

alternative unique database field (ie, not the primary id field).


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3470 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 16 additions and 0 deletions
lib/ktentity.inc
@@ -289,6 +289,22 @@ class KTEntityUtil { @@ -289,6 +289,22 @@ class KTEntityUtil {
289 } 289 }
290 return $oObject; 290 return $oObject;
291 } 291 }
  292 +
  293 + function &getBy($sClassName, $sField, $mValue) {
  294 + $sTable = call_user_func(array($sClassName, "_table"));
  295 + $sWhereClause = array("$sField = ?", array($mValue));
  296 + $aObjects =& KTEntityUtil::getList($sTable, $sClassName, $sWhereClause);
  297 + if (PEAR::isError($aObjects)) {
  298 + return $aObjects;
  299 + }
  300 + if (count($aObjects) === 0) {
  301 + return PEAR::raiseError("No objects returned");
  302 + }
  303 + if (count($aObjects) > 1) {
  304 + return PEAR::raiseError("Multiple objects returned");
  305 + }
  306 + return $aObjects[0];
  307 + }
292 } 308 }
293 309
294 ?> 310 ?>