Commit 09041a3126627190b4feeabf2ba91ac9d46f2211
1 parent
78102581
Expand KTEntity to simplify get() and support loading, creating, and
updating objects using dictionaries. git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3456 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
171 additions
and
2 deletions
lib/ktentity.inc
| ... | ... | @@ -27,6 +27,7 @@ |
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | 29 | class KTEntity { |
| 30 | + var $_bUsePearError = false; | |
| 30 | 31 | /** object primary key */ |
| 31 | 32 | var $iId; |
| 32 | 33 | |
| ... | ... | @@ -40,8 +41,12 @@ class KTEntity { |
| 40 | 41 | if ($this->iId < 0) { |
| 41 | 42 | $id = DBUtil::autoInsert($this->_table(), $this->_fieldValues()); |
| 42 | 43 | if (PEAR::isError($id)) { |
| 43 | - $_SESSION["errorMessage"] = $id->toString(); | |
| 44 | - return false; | |
| 44 | + if ($this->_bUsePearError === false) { | |
| 45 | + $_SESSION["errorMessage"] = $id->toString(); | |
| 46 | + return false; | |
| 47 | + } else { | |
| 48 | + return $id; | |
| 49 | + } | |
| 45 | 50 | } |
| 46 | 51 | $this->iId = $id; |
| 47 | 52 | return true; |
| ... | ... | @@ -87,6 +92,131 @@ class KTEntity { |
| 87 | 92 | $_SESSION["errorMessage"] = "Can't delete an object that isn't in the database";; |
| 88 | 93 | return false; |
| 89 | 94 | } |
| 95 | + | |
| 96 | + function _getSqlSelection() { | |
| 97 | + $aRet = array(); | |
| 98 | + foreach ($this->_aFieldToSelect as $k => $v) { | |
| 99 | + $aRet[] = $v; | |
| 100 | + } | |
| 101 | + return join(", ", $aRet); | |
| 102 | + } | |
| 103 | + | |
| 104 | + function load($iId = null) { | |
| 105 | + if (is_null($iId)) { | |
| 106 | + if (is_null($this->iId)) { | |
| 107 | + return PEAR::raiseError("No ID given"); | |
| 108 | + } | |
| 109 | + $iId = $this->iId; | |
| 110 | + } | |
| 111 | + $table = $this->_table(); | |
| 112 | + $select = $this->_getSqlSelection(); | |
| 113 | + $sQuery = "SELECT $select FROM $table WHERE id = ?"; | |
| 114 | + $aParams = array($iId); | |
| 115 | + | |
| 116 | + $res = DBUtil::getResultArray(array($sQuery, $aParams)); | |
| 117 | + | |
| 118 | + if (PEAR::isError($res)) { | |
| 119 | + return $res; | |
| 120 | + } | |
| 121 | + if (count($res) === 0) { | |
| 122 | + return PEAR::raiseError("No such ID"); | |
| 123 | + } | |
| 124 | + if (count($res) > 1) { | |
| 125 | + return PEAR::raiseError("Multiple matches for ID"); | |
| 126 | + } | |
| 127 | + $vk = array_flip($this->_aFieldToSelect); | |
| 128 | + $aLoadInfo = array(); | |
| 129 | + foreach ($res[0] as $k => $v) { | |
| 130 | + $aLoadInfo[$vk[$k]] = $v; | |
| 131 | + } | |
| 132 | + $res = $this->loadFromArray($aLoadInfo); | |
| 133 | + if (PEAR::isError($res)) { | |
| 134 | + return $res; | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + function loadFromArray ($aOptions) { | |
| 139 | + if (!is_array($aOptions)) { | |
| 140 | + return PEAR::raiseError("Expected an array!"); | |
| 141 | + } | |
| 142 | + | |
| 143 | + foreach ($aOptions as $sField => $sValue) { | |
| 144 | + $sElement = $this->_getElementFromMethod($sField); | |
| 145 | + if ($sElement === false) { | |
| 146 | + return PEAR::raiseError('Setting a non-existent field: ' . $sField); | |
| 147 | + } | |
| 148 | + if (PEAR::isError($sElement)) { | |
| 149 | + return $sElement; | |
| 150 | + } | |
| 151 | + $ret = $this->_set($sElement, $sValue); | |
| 152 | + if (PEAR::isError($ret)) { | |
| 153 | + return $ret; | |
| 154 | + } | |
| 155 | + } | |
| 156 | + return true; | |
| 157 | + } | |
| 158 | + | |
| 159 | + function &_set (&$element, &$params) { | |
| 160 | + $this->$element = $params; | |
| 161 | + return array(true, true); | |
| 162 | + } | |
| 163 | + | |
| 164 | + function &_getElementFromMethod ($sElement) { | |
| 165 | + // The element is probably lower-case, for various reasons. Get | |
| 166 | + // the correct case from the aFieldToSelect dictionary's keys. | |
| 167 | + // | |
| 168 | + // If the element isn't in the case array, the method doesn't | |
| 169 | + // exist. | |
| 170 | + | |
| 171 | + $array = array_keys($this->_aFieldToSelect); | |
| 172 | + | |
| 173 | + foreach($array as $k) { | |
| 174 | + $case[strtolower($k)] = $k; | |
| 175 | + } | |
| 176 | + | |
| 177 | + $sElement = strtolower($sElement); | |
| 178 | + | |
| 179 | + if (array_key_exists($sElement, $case)) { | |
| 180 | + return $case[strtolower($sElement)]; | |
| 181 | + } | |
| 182 | + | |
| 183 | + foreach($array as $k) { | |
| 184 | + $case[substr(strtolower($k), 1)] = $k; | |
| 185 | + } | |
| 186 | + | |
| 187 | + if (array_key_exists($sElement, $case)) { | |
| 188 | + return $case[strtolower($sElement)]; | |
| 189 | + } | |
| 190 | + return PEAR::raiseError("No such element"); | |
| 191 | + } | |
| 192 | + | |
| 193 | + function _fieldValues () { | |
| 194 | + $aRet = array(); | |
| 195 | + | |
| 196 | + foreach ($this->_aFieldToSelect as $k => $v) { | |
| 197 | + if ($k === 'iId') { | |
| 198 | + continue; | |
| 199 | + } | |
| 200 | + $aRet[$v] = $this->$k; | |
| 201 | + } | |
| 202 | + return $aRet; | |
| 203 | + } | |
| 204 | + | |
| 205 | + function updateFromArray ($aOptions) { | |
| 206 | + $ret = $this->load(); | |
| 207 | + if (PEAR::isError($ret)) { | |
| 208 | + return $ret; | |
| 209 | + } | |
| 210 | + $ret = $this->loadFromArray($aOptions); | |
| 211 | + if (PEAR::isError($ret)) { | |
| 212 | + return $ret; | |
| 213 | + } | |
| 214 | + $ret = $this->update(); | |
| 215 | + if (PEAR::isError($ret)) { | |
| 216 | + return $ret; | |
| 217 | + } | |
| 218 | + return true; | |
| 219 | + } | |
| 90 | 220 | } |
| 91 | 221 | |
| 92 | 222 | class KTEntityUtil { |
| ... | ... | @@ -120,6 +250,45 @@ class KTEntityUtil { |
| 120 | 250 | } |
| 121 | 251 | return $aRet; |
| 122 | 252 | } |
| 253 | + | |
| 254 | + function createFromArray ($sClassName, $aOptions) { | |
| 255 | + $oObject = new $sClassName; | |
| 256 | + $ret = $oObject->loadFromArray($aOptions); | |
| 257 | + if (PEAR::isError($ret)) { | |
| 258 | + return $ret; | |
| 259 | + } | |
| 260 | + $ret = $oObject->create(); | |
| 261 | + if (PEAR::isError($ret)) { | |
| 262 | + return $ret; | |
| 263 | + } | |
| 264 | + return true; | |
| 265 | + } | |
| 266 | + | |
| 267 | + function updateFromArray ($sClassName, $iId, $aOptions) { | |
| 268 | + $oObject = new $ClassName; | |
| 269 | + $ret = $oObject->load($iId); | |
| 270 | + if (PEAR::isError($ret)) { | |
| 271 | + return $ret; | |
| 272 | + } | |
| 273 | + $ret = $this->loadFromArray($aOptions); | |
| 274 | + if (PEAR::isError($ret)) { | |
| 275 | + return $ret; | |
| 276 | + } | |
| 277 | + $ret = $this->update(); | |
| 278 | + if (PEAR::isError($ret)) { | |
| 279 | + return $ret; | |
| 280 | + } | |
| 281 | + return true; | |
| 282 | + } | |
| 283 | + | |
| 284 | + function &get($sClassName, $iId) { | |
| 285 | + $oObject =& new $sClassName; | |
| 286 | + $res = $oObject->load($iId); | |
| 287 | + if (PEAR::isError($res)) { | |
| 288 | + return $res; | |
| 289 | + } | |
| 290 | + return $oObject; | |
| 291 | + } | |
| 123 | 292 | } |
| 124 | 293 | |
| 125 | 294 | ?> | ... | ... |