Model: I have a table like id:name:parentId. The parentId references
to the id. Simple recursion to build a tree.
In example:
- 1:root:null
- 2:Heading1:1
- 3:Heading2:1
- 4:subheading1.1:2
In my controller I would now call the Database and pass the data
through to the view. If I would do that then I would have a function
inside my view. I do not like functionality in my view :).
The other approach would be to sort that somehow. But would this really make a difference?
- For doing that I would have to use helping fields to know which level I am in. So I could iterate over the sorted array.
- Or the other approach: Multidimensional arrays. Maybe pointless because I have no idea how deep that tree is going to be.
Would be thankful for any recommendations.
Thanks in advance.
edit: im receiving the data as objects.
According to the trends in MVC framework based designs (fat model, slim controller), I would recommend to use your Model to fetch the data and deliver a nested data set (a mulitidimensional array) to the controller/view. It doesn’t matter how deep the hierarchy will be, it will be finite.
As the question is a bit broadly asked, it’s not clear what sort of data it is, neither whether you’re always going to need the whole data at once or just some relative chunk of it. Either way I suppose you’d like to keep the hierarchy, so if you’re not comfortable iterating through nested arrays there’s always the alternative of using (Std)Objects.