/  5
 
1.
// pseudo controller code
2.
$data['Category']['parent_id'] =3;
3.
$data['Category']['name'] ='Skating';
4.
$this->Category->save($data);
1.
// pseudo controller code
2.
$data= array();
3.
$data['Category']['name'] ='Other People\'s Categories';
4.
$this->Category->save($data);
1.
// pseudo controller code
2.
$this->Category->id=5;// id of Extreme knitting
3.
$this->Category->save(array('name'=>'Extreme fishing'));
Extreme knittingFriendsGeraldGwendolynWorkReportsAnnualStatusTripsNationalInternational
6.4.2.1
(#Adding-data-520)
Adding data
In the previous section, we used existing data and checked that it looked hierarchal via the method
generatetreelist
. However, usually you would add your data in exactly the same way as you would for any model.For example:
Plain Text View
(#)
When using the tree behavior its not necessary to to do any more than set the parent_id, and the tree behavior willtake care of the rest. If you don't set the parent_id, the tree behavior will add to the tree making your new addition anew top level entry:
Plain Text View
(#)
Running the above two code snippets would alter your tree as follows:My CategoriesFunSportSurfingExtreme knittingSkating
New
FriendsGeraldGwendolynWorkReportsAnnualStatusTripsNationalInternationalOther People's Categories
New
6.4.2.2
(#Modifying-data-521)
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:
Plain Text View
(#)
The above code did not affect the parent_id field - even if the parent_id is included in the data that is passed to saveif the value doesn't change, neither does the data structure. Therefore the tree of data would now look like:My CategoriesFunSportSurfing
The Cookbook :: 1.2 Collection :: The Manual :: Core Behav...http://book.cakephp.org/view/228/Basic-Usage2 of 509/28/2008 01:19 PM
 
1.
// pseudo controller code
2.
$this->Category->id=5;// id of Extreme fishing
3.
$newParentId=$this->field('id', array('name'=>'Other People\'s Categories'));
4.
$this->Category->save(array('parent_id'=>$newParentId));
1.
// pseudo controller code
2.
$this->Category->id=10;
3.
$this->Category->delete();
Extreme fishing
Updated
SkatingFriendsGeraldGwendolynWorkReportsAnnualStatusTripsNationalInternationalOther People's CategoriesMoving 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:
Plain Text View
(#)
As would be expected the structure would be modified to:My CategoriesFunSportSurfingSkatingFriendsGeraldGwendolynWorkReportsAnnualStatusTripsNationalInternationalOther People's CategoriesExtreme fishing
Moved
6.4.2.3
(#Deleting-data-522)
Deleting data
The tree behavior provides a number of ways to manage deleting data. To start with the simplest example; let's saythat the reports category is no longer useful. To remove it
and any children it may have 
just call delete as you wouldfor any model. For example with the following code:
Plain Text View
(#)
The category tree would be modified as follows:My CategoriesFunSportSurfingSkatingFriendsGeraldGwendolynWorkTripsNationalInternationalOther People's CategoriesExtreme fishing
6.4.2.4
(#Querying-and-using-your-data-523)
Querying and using your data
The Cookbook :: 1.2 Collection :: The Manual :: Core Behav...http://book.cakephp.org/view/228/Basic-Usage3 of 509/28/2008 01:19 PM

Share & Embed

More from this user

Add a Comment

Characters: ...