PHP Doctrine 1.2 Using Nested Set , how moving a node in different situation

1.2k Views Asked by At

I use nested set with doctrine 1.2.

Here is my example.

I got this tree

Category 1
   Category 1.1
   Category 1.2
   Category 1.3
   Category 1.4
Category 2
   Category 2.1
      Category 2.1.1
      Category 2.1.2
      Category 2.1.3

Situation
1 - how can I Move the Category 1.3 On top of Category 1.1
2 - How can I Move Category 1.4 INSIDE the Category 1.3
3 - How Can I Move 2.1 and his child INSIDE Category 1 and next to Category 1.1

Situation 1 Will give me:

Category 1
   Category 1.3
   Category 1.1
   Category 1.2
   Category 1.4
...

Situation 2 will give me:

Category 1
   Category 1.1
   Category 1.2
   Category 1.3
      Category 1.4
...

Situation 3 will give me:

Category 1
   Category 1.1
   Category 2
      Category 2.1
         Category 2.1.1
         Category 2.1.2
         Category 2.1.3
   Category 1.2
   Category 1.3
   Category 1.4
1

There are 1 best solutions below

0
On BEST ANSWER

See http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_node_interface.html

Please note that your original question was not entirely correct: in case 3) you said you only wanted to move 2.1 inside Category 1, but according to the sample you have given for that scenario, you actually wanted to move all of Category 2 inside Category 1.

Let's say the names you mentioned (Category 1.1) is an actual ID of the category, here are the scenarios you are describing:

$cat11 = Doctrine_Core::getTable("Category")->find("Category 1.1");
$cat13 = Doctrine_Core::getTable("Category")->find("Category 1.3");
$cat14 = Doctrine_Core::getTable("Category")->find("Category 1.4");
$cat21 = Doctrine_Core::getTable("Category")->find("Category 2.1");

Moving 1.3 on top of category 1.1:

$cat13->getNode()->moveAsPrevSiblingOf($cat11);

Moving 1.4 inside 1.3:

$cat14->getNode()->moveAsFirstChildOf($cat13);

Moving all of Category 2 inside category 1, next to Category 1.1:

$cat21->getNode()->moveAsNextSiblingOf($cat11);

Don't forget to save the categories after the manipulation, e.g. $cat11->save(). It's sufficient to save the category you were manipulating.