Welcome to the Cookbook

loading...

6.4.2.2 Modifying data

Modifying data is as transparent as adding new data. If you modify something, but do not change the parent_id field - the structure of your data will remain unchanged. For example:

// pseudo controller code
$this->Category->id = 5; // id of Extreme knitting
$this->Category->save(array('name' =>'Extreme fishing'));
  1. // pseudo controller code
  2. $this->Category->id = 5; // id of Extreme knitting
  3. $this->Category->save(array('name' =>'Extreme fishing'));

The above code did not affect the parent_id field - even if the parent_id is included in the data that is passed to save if the value doesn't change, neither does the data structure. Therefore the tree of data would now look like:

  • My Categories
    • Fun
      • Sport
        • Surfing
        • Extreme fishing Updated
        • Skating
      • Friends
        • Gerald
        • Gwendolyn
    • Work
      • Reports
        • Annual
        • Status
      • Trips
        • National
        • International
  • Other People's Categories

Moving data around in your tree is also a simple affair. Let's say that Extreme fishing does not belong under Sport, but instead should be located under Other People's Categories. With the following code:

// pseudo controller code
$this->Category->id = 5; // id of Extreme fishing
$newParentId = $this->Category->field('id', array('name' => 'Other People\'s Categories'));
$this->Category->save(array('parent_id' => $newParentId)); 
  1. // pseudo controller code
  2. $this->Category->id = 5; // id of Extreme fishing
  3. $newParentId = $this->Category->field('id', array('name' => 'Other People\'s Categories'));
  4. $this->Category->save(array('parent_id' => $newParentId));

As would be expected the structure would be modified to:

  • My Categories
    • Fun
      • Sport
        • Surfing
        • Skating
      • Friends
        • Gerald
        • Gwendolyn
    • Work
      • Reports
        • Annual
        • Status
      • Trips
        • National
        • International
  • Other People's Categories
    • Extreme fishing Moved