Você está na página 1de 5

The Cookbook :: 1.2 Collection :: The Manual :: Core Behav... http://book.cakephp.

org/view/228/Basic-Usage

1 of 5 09/28/2008 01:19 PM
The Cookbook :: 1.2 Collection :: The Manual :: Core Behav... http://book.cakephp.org/view/228/Basic-Usage

Extreme knitting
Friends
Gerald
Gwendolyn
Work
Reports
Annual
Status
Trips
National
International

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 (#)

1. // pseudo controller code


2. $data['Category']['parent_id'] = 3;
3. $data['Category']['name'] = 'Skating';
4. $this->Category->save($data);

When using the tree behavior its not necessary to to do any more than set the parent_id, and the tree behavior will
take care of the rest. If you don't set the parent_id, the tree behavior will add to the tree making your new addition a
new top level entry:

Plain Text View (#)

1. // pseudo controller code


2. $data = array();
3. $data['Category']['name'] = 'Other People\'s Categories';
4. $this->Category->save($data);

Running the above two code snippets would alter your tree as follows:

My Categories
Fun
Sport
Surfing
Extreme knitting
Skating New
Friends
Gerald
Gwendolyn
Work
Reports
Annual
Status
Trips
National
International
Other 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 (#)

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

2 of 5 09/28/2008 01:19 PM
The Cookbook :: 1.2 Collection :: The Manual :: Core Behav... http://book.cakephp.org/view/228/Basic-Usage

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:

Plain Text View (#)

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));

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

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 say
that the reports category is no longer useful. To remove it and any children it may have just call delete as you would
for any model. For example with the following code:

Plain Text View (#)

1. // pseudo controller code


2. $this->Category->id = 10;
3. $this->Category->delete();

The category tree would be modified as follows:

My Categories
Fun
Sport
Surfing
Skating
Friends
Gerald
Gwendolyn
Work
Trips
National
International
Other People's Categories
Extreme fishing

6.4.2.4 (#Querying-and-using-your-data-523) Querying and using your data

3 of 5 09/28/2008 01:19 PM
The Cookbook :: 1.2 Collection :: The Manual :: Core Behav... http://book.cakephp.org/view/228/Basic-Usage

Using and manipulating hierarchical data can be a tricky business. In addition to the core find methods, with the tree
behavior there are a few more tree-orientated permutations at your disposal.

Most tree behavior methods return and rely on data being sorted by the lft field. If you call find() and do not order
by lft, or call a tree behavior method and pass a sort order, you may get undesirable results.

6.4.2.4.1 (#Children-525) Children

The children method takes the primary key value (the id) of a row and returns the children, by default in the order
they appear in the tree. The second optional parameter defines whether or not only direct children should be
returned. Using the example data from the previous section:

Plain Text View (#)

1. $allChildren = $this->Category->children(1); // a flat array with 11 items


2. // -- or --
3. $this->Category->id = 1;
4. $allChildren = $this->Category->children(); // a flat array with 11 items
5. // Only return direct children
6. $directChildren = $this->Category->children(1, true); // a flat array with 2 items

If you want a recursive array use find('threaded')

6.4.2.4.2 (#Counting-children-239) Counting children

As with the method children, childCount takes the primary key value (the id) of a row and returns how many children
it has. The second optional parameter defines whether or not only direct children are counted. Using the example
data from the previous section:

Plain Text View (#)

1. $totalChildren = $this->Category->childCount(1); // will output 11


2. // -- or --
3. $this->Category->id = 1;
4. $directChildren = $this->Category->childCount(); // will output 11
5. // Only counts the direct descendants of this category
6. $numChildren = $this->Category->childCount(1, true); // will output 2

6.4.2.4.3 (#generatetreelist-517) generatetreelist

This method will return data similar to find('list'), with an indented prefix to show the structure of your data. Below is
an example of what you can expect this method to return see the api for the other find-like parameters.

array(
[1] => "My Categories",
[2] => "_Fun",
[3] => "__Sport",
[4] => "___Surfing",
[16] => "___Skating",
[6] => "__Friends",
[7] => "___Gerald",
[8] => "___Gwendolyn",
[9] => "_Work",
[13] => "__Trips",
[14] => "___National",
[15] => "___International",
[17] => "Other People's Categories",
[5] => "_Extreme fishing"
)

6.4.2.4.4 (#getparentnode-236) getparentnode

This convenience function will, as the name suggests, return the parent node for any node, or false if the node has
no parent (its the root node). For example:

Plain Text View (#)

1. $parent = $this->Category->getparentnode(2); //<- id for fun


2. // $parent contains All categories

6.4.2.4.5 (#getpath-235) getpath

4 of 5 09/28/2008 01:19 PM
The Cookbook :: 1.2 Collection :: The Manual :: Core Behav... http://book.cakephp.org/view/228/Basic-Usage

(http://creativecommons.org/licenses/by-nc-nd/3.0/)

© Cake Software Foundation, Inc. (http://cakefoundation.org)

5 of 5 09/28/2008 01:19 PM

Você também pode gostar