Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy tree structure recursive - Adjacency List Model
    primarykey
    data
    text
    <p>I try to copy a tree structure from one database table to another. The structure is an Adjacency List Model. It looks like:</p> <pre><code>id|parent_id|position 1|0|1 2|1|1 3|1|2 4|0|2 5|4|1 </code></pre> <p>It is <strong>necessary</strong> that the id's are regenerated (autoinc) in the other table! I have the following functions:</p> <pre><code>/** * Copy a single node and return the new id */ public function copyNode($sn_data){ $this-&gt;db2-&gt;insert('items_configurations', $sn_data); return $this-&gt;db2-&gt;insert_id(); } /** * Return a list of child nodes as an assoziative array * from a given parent */ public function childList($parent_id){ $tmp = 'SELECT parent_id,item_id,template_id,position FROM items_templates WHERE parent_id='.$parent_id; $tmp .= ' ORDER BY position'; $query=$this-&gt;db2-&gt;query($tmp); return $query-&gt;result_array(); } /** * Copy the whole tree structure through an recursive function */ public function copyTree($node_data,$given_parent){ $new_parent = $this-&gt;copyNode($node_data); $new_data = $this-&gt;childList($node_data['id']); if(is_array($new_data)){ foreach($new_data as $new_node_data) : $new_node_data['parent_id'] = $given_parent; $new_node_data['configuration_id'] = $node_data['configuration_id']; $this-&gt;copyTree($new_node_data,$new_parent); endforeach; } } /** * First call of the function for example: */ $this-&gt;copyTree(array('parent_id' =&gt; 0,'item_id' =&gt; 40,'template_id' =&gt; 6,'position' =&gt; 1),0); </code></pre> <p>I want to do it recursive, but it only copies the first two lines. Where is the mistake?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload