I’m trying to load my comments for articles from the Comments table.
The table has two columns relevant to this:
id (id of the comment)
reply (id of the comment being replied to, null if not a reply)
Now my question is how I can contain the comments but have them be recursive.
eg. the first comment is not a reply, but has two replies, one of which has a reply, which in term has it’s own reply.
Just create an association from Comments to itself? But if there’s effectively no limit to the level of recursion, you won’t be able to specify enough ['Comments' => ['Comments' => ['Comments' => levels to be sure to get them all. You’d need to use some sort of lazy loading instead, maybe with a plugin like this one, or just calling loadInto in an accessor function.
I’ve managed to get the LazyLoad plugin to partially work, however, I can only get the first level of replies with this as well :\
I guess that for now I’ll have to abandon this idea (and instead just have 1 huge line of comments, in a fashion we have here as well as opposed to the “reddit style” of comments) for now.
I stashed it right now (tho it’s at my work PC so I don’t have access to it right now) while I continued on the “forum style” comments but here is what it looked like:
Assuming that your display code here is from memory, as it has a couple of obvious errors in it. Your data structure, after loading, would look like $article->comments[0]->replies[0]->replies[0], etc. You probably need to make an element that would output a comment and it’s replies, where the replies are in turn outputted with the same element.
Not sure what you mean about limiting itself. If you only want to show a certain number of levels of replies, then you could pass 'level' => 1 to the first invocation, and 'level' => $level + 1 to the recursive call, and use that to decide whether to continue to another level or not.
That seems to work pretty fine
Though I’ve (on advice of one of my co-workers) hardcoded it to limit to 10 “threads” max to prevent the famous Reddit issue in /r/counting :^)