How do I generate lft and rght values for a cakephp 2.0 tree?

孤者浪人 提交于 2019-12-23 02:45:09

问题


I've read that if you have a model that $actsAs = array('Tree'), that you can simply set the parent_id for your seed data, and a call to $this->Model->recover() should generate the proper lft and rght values for you, but when I do this, Cake seems to generate random values every time. The values vary from very large (in the thousands) to negative values. I've checked for circular references and found none as well. What could be wrong here?

SQL:

create table menus (
   id        int auto_increment not null,
   parent_id int                null    ,
   lft       int                null    ,
   rght      int                null    ,
   title     varchar(1024)      not null,
   path      varchar(1024)      not null,
   constraint pk_menus primary key (id)
)   ENGINE=InnoDB DEFAULT CHARSET=latin1;

This is the seeded data:          After one Model->recover()
+----+-----------+------+------+  +----+-----------+------+------+
| id | parent_id | lft  | rght |  | id | parent_id | lft  | rght |
+----+-----------+------+------+  +----+-----------+------+------+
|  1 |      NULL | NULL | NULL |  |  1 |      NULL |  595 |  619 |
|  7 |         1 | NULL | NULL |  |  7 |         1 |  619 |  567 |
|  6 |         1 | NULL | NULL |  |  6 |         1 |  627 |  595 |
|  5 |         1 | NULL | NULL |  |  5 |         1 |  600 |  621 |
|  4 |         1 | NULL | NULL |  |  4 |         1 |  603 |  621 |
|  3 |         1 | NULL | NULL |  |  3 |         1 |  619 |  529 |
|  2 |         1 | NULL | NULL |  |  2 |         1 |  595 |  529 |
|  8 |         2 | NULL | NULL |  |  8 |         2 |  627 |  628 |
| 13 |         3 | NULL | NULL |  | 13 |         3 |  595 |  567 |
| 12 |         3 | NULL | NULL |  | 12 |         3 |  627 |  621 |
| 11 |         3 | NULL | NULL |  | 11 |         3 |  595 |  631 |
| 10 |         3 | NULL | NULL |  | 10 |         3 |  604 |  529 |
|  9 |         3 | NULL | NULL |  |  9 |         3 |  595 |  567 |
| 14 |         5 | NULL | NULL |  | 14 |         5 |  628 |  629 |
| 15 |         5 | NULL | NULL |  | 15 |         5 |  567 |  529 |
| 16 |         5 | NULL | NULL |  | 16 |         5 |  619 |  600 |
| 17 |         5 | NULL | NULL |  | 17 |         5 |  627 |  605 |
| 21 |         6 | NULL | NULL |  | 21 |         6 |  567 |  619 |
| 20 |         6 | NULL | NULL |  | 20 |         6 |  595 |  567 |
| 19 |         6 | NULL | NULL |  | 19 |         6 |  619 |  600 |
| 18 |         6 | NULL | NULL |  | 18 |         6 |  567 |  529 |
| 22 |        10 | NULL | NULL |  | 22 |        10 |  567 |  619 |
| 23 |        11 | NULL | NULL |  | 23 |        11 |   64 |  621 |
| 24 |        12 | NULL | NULL |  | 24 |        12 |  627 |  621 |
| 25 |        13 | NULL | NULL |  | 25 |        13 |  605 |  595 |
| 32 |        16 | NULL | NULL |  | 32 |        16 |  628 |  627 |
| 31 |        16 | NULL | NULL |  | 31 |        16 |  567 |  619 |
| 30 |        16 | NULL | NULL |  | 30 |        16 |   64 |  621 |
| 29 |        16 | NULL | NULL |  | 29 |        16 |  567 |  619 |
| 28 |        16 | NULL | NULL |  | 28 |        16 |  595 |  567 |
| 27 |        16 | NULL | NULL |  | 27 |        16 |  627 |  621 |
| 26 |        16 | NULL | NULL |  | 26 |        16 |  605 |  595 |
| 33 |        17 | NULL | NULL |  | 33 |        17 |  567 |  619 |
| 37 |        19 | NULL | NULL |  | 37 |        19 |   52 |  619 |
| 36 |        19 | NULL | NULL |  | 36 |        19 |   52 |  619 |
| 35 |        19 | NULL | NULL |  | 35 |        19 |  595 |  619 |
| 34 |        19 | NULL | NULL |  | 34 |        19 |   63 |  621 |
| 38 |        20 | NULL | NULL |  | 38 |        20 |   63 |  621 |
| 39 |        20 | NULL | NULL |  | 39 |        20 |   63 |  621 |
| 40 |        21 | NULL | NULL |  | 40 |        21 |  605 |  595 |
+----+-----------+------+------+  +----+-----------+------+------+

回答1:


I found that if I set the lft and rght values of the top level item to 1 and 2 respectively, recover() now generates the rest of the values properly. I find this to be a bug, but not a terrible one.



来源:https://stackoverflow.com/questions/8496084/how-do-i-generate-lft-and-rght-values-for-a-cakephp-2-0-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!