Commit b800d3deaa34428e67de1031b54d7be175c9ca10
1 parent
fe4f0b35
Add a helper method to KTEntity-based objects. newCopy will make a new
copy of the object with a new id. This is the preferred way to create a "copy" of an object. git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@5269 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
13 additions
and
0 deletions
lib/ktentity.inc
| @@ -111,6 +111,19 @@ class KTEntity { | @@ -111,6 +111,19 @@ class KTEntity { | ||
| 111 | return false; | 111 | return false; |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | + function newCopy() { | ||
| 115 | + $sClass = get_class($this); | ||
| 116 | + $oObject =& new $sClass; | ||
| 117 | + foreach (array_keys($this->_aFieldToSelect) as $k) { | ||
| 118 | + $oObject->$k = $this->$k; | ||
| 119 | + } | ||
| 120 | + $oObject->iId = -1; | ||
| 121 | + $oObject->create(); | ||
| 122 | + | ||
| 123 | + $iId = $oObject->iId; | ||
| 124 | + return KTEntityUtil::get($sClass, $iId); | ||
| 125 | + } | ||
| 126 | + | ||
| 114 | /** | 127 | /** |
| 115 | * Update the values in the database table with the object's current values | 128 | * Update the values in the database table with the object's current values |
| 116 | * | 129 | * |