· 8 years ago · Jun 02, 2018, 06:24 AM
1// controller
2
3 public function tree($create = null) {
4 if ($create) {
5 $moto = new Category_Model();
6 $moto->name = "Moto";
7 $moto->save();
8
9 $os = new Category_Model();
10 $os->name = "osobowe";
11 $os->parent_id = $moto->id;
12 $os-> save();
13
14 $ciez = new Category_Model();
15 $ciez->name = "ciezarowe";
16 $ciez->parent_id = $moto->id;
17 $ciez-> save();
18
19 $tiry = new Category_Model();
20 $tiry->name = "tiry";
21 $tiry->parent_id = $ciez->id;
22 $tiry-> save();
23
24
25 $os = new Category_Model();
26 $os->name = "scania";
27 $os->parent_id = $tiry->id;
28 $os-> save();
29
30 $os = new Category_Model();
31 $os->name = "man";
32 $os->parent_id = $tiry->id;
33 $os-> save();
34
35 }
36
37 function rec($o) {
38 echo "<ul>";
39
40 foreach ($o->children as $c) {
41 echo "<li>".$c->name;
42 rec($c);
43 echo "</li>";
44
45 }
46 echo "</ul>";
47 }
48
49 $m = ORM::factory('Category', 1);
50
51 rec($m);
52 $this->auto_render = false;
53 }
54
55
56
57
58
59
60// model
61class Category_Model extends ORM_Tree {
62 protected $children = "categories";
63}
64
65
66
67
68// sql
69
70CREATE TABLE IF NOT EXISTS `categories` (
71 `id` int(11) NOT NULL auto_increment,
72 `parent_id` int(11) NOT NULL,
73 `name` varchar(100) NOT NULL,
74 PRIMARY KEY (`id`)
75) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;