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
Add a Comment
borninfreeleft a comment