DataObjects/Core_enum.php
[Pman.Core] / DataObjects / Core_enum.php
index 6df7df4..3a3b6e2 100644 (file)
@@ -125,7 +125,7 @@ class Pman_Core_DataObjects_Core_enum extends DB_DataObject
     function beforeUpdate($old, $request,$roo)
     {   
         if(!empty($request['_merge_id'])){
-            $this->merge($request['_merge_id']);
+            $this->merge($request['_merge_id'], $roo);
         }
         
         $tn = $this->tableName();
@@ -338,24 +338,45 @@ class Pman_Core_DataObjects_Core_enum extends DB_DataObject
         
     }
     
-    function merge($id)
+    function merge($to, $roo)
     {
-        $map = $this->links();
-        
         $affects  = array();
         
         $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$this->_database];
-        print_R($all_links);exit;
+        
         foreach($all_links as $tbl => $links) {
             foreach($links as $col => $totbl_col) {
                 $to = explode(':', $totbl_col);
-                if ($to[0] != $x->tableName()) {
+                if ($to[0] != $this->tableName()) {
                     continue;
                 }
                 
                 $affects[$tbl .'.' . $col] = true;
             }
         }
+        
+        foreach($affects as $k => $true) {
+            $ka = explode('.', $k);
+
+            $chk = DB_DataObject::factory($ka[0]);
+            
+            if (!is_a($chk,'DB_DataObject')) {
+                $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
+            }
+            
+            $chk->{$ka[1]} = $this->id;
+
+            foreach ($chk->fetchAll() as $c){
+                $cc = clone ($c);
+                $c->{$ka[1]} = $to;
+                $c->update($cc);
+            }
+        }
+        
+        $this->delete();
+        
+        $roo->jok('Merged');
+        
     }