G
G
Grigory Vasilkov2016-12-21 23:16:03
opencart
Grigory Vasilkov, 2016-12-21 23:16:03

Storing trees - why in Opencart in the table of parent-child relationships are the levels “reverse”?

Until today, I always thought that if you store nesting, you need to write like this:
>> category
id, parent, name
1, null, cat1,
2,1, cat2,
3,2, cat3
4,3, cat4
>> catnest
id, parent_id,level
1,1,0
2,1,1
2,2,0
3,1,2
3,2,1,
3,3,0,
4,1,3
4,2,2
4,3,1 ,
4,4,0
For some reason, the levels are reversed in OPEN CART. That is, the creator stores the difference between the absolute nesting level and the relative one .
That is, in opencart, catnest looks like this:
>> catnest
id, parent_id, level
1,1,0
2,1,0
2,2,1
3,1,0
3,2,1,
3,3,2,
4,1,0
4,2,1
4,3,2,
4,4,3 Is
there a rational grain in this or is it the creator's stupidity? Why might levels "reversely" be needed when sorting?
PS1. In the course of practice, it turned out that such storage is useful only in the case of storing a tree of comments - elements from SQL are immediately returned sorted in the form of a tree by identifier - and the identifier, as you know, reflects the date the element was created. In the comments, this is how it should be - sort by nesting, by date, and quickly . But if you need to display a list of categories or a list of categories with products sorted by price , the method fails miserably.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Александр Аксентьев, 2016-12-21
@Sanasol

https://github.com/opencart/opencart
посмотреть исходники и понять почему так.
спросить у авторов почему так.

Денис, 2016-12-21
@prototype_denis

Выбор метода хранения деревьев очень сильно зависит от того как будут использоваться данные. В данном случае логично предположить, что такой способ самый оптимальный.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question