A
A
Arman2017-02-26 10:14:11
Yii
Arman, 2017-02-26 10:14:11

How to draw a Nested sets tree with all entries?

DD.
I use https://github.com/creocoder/yii2-nested-sets
If through recursion it turns out to be easy to draw by requesting descendants one after another with the "depth = 1" constraint, but a lot of requests come out.
I would like to get all the records with one request and somehow draw them, I usually saw that they use parent_id, but there is no such thing here.
Or is it possible to somehow make connections so that he builds connections with 2-3 requests? Now under 30-40 requests to render everything.
Or maybe refuse it, but then what is better? Need to make a list of sections of 3-4 levels with 50-60 items

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Timofeev, 2017-02-26
@Arik

$myModel = MyModel::find()->orderBy('lft')->all();
Further recursion in php. 1 request. The trick is sorting by lft. Then you already have the correct ordered structure, it only remains for it to nest based on depth.
Alternatively, you can look here:
https://github.com/yiiext/nested-set-behavior/blob...
there is a php example at the bottom. It is for yii1, but in fact everything is the same

T
ThunderCat, 2017-02-26
@ThunderCat

like nothing unusual, make a request and select all records, then make a recursive function in php and build a tree, here instead of parent_id, as far as I understand, the indication lft is used - the element on the left is apparently the parent, and rgt is the child element. The function is searched by the search engine in a minute, at the request of the recursion tree nested sets.

D
Dmitry, 2017-02-26
@slo_nik

Good morning.
In ActiveRecord, you can make links from one model to another. If you are talking about this, then look here (Working with connected data).

P
PaulZi, 2017-04-05
@PaulZi

If you are interested in my nested sets extension , there is a populateTree() method, which just allows you to request descendants from the database with one request and update their connections.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question