Adding custom attributes to comments
Jamroom Developers
I usually play round with concepts to find one that works in code, then start thinking about how efficient its going to be once I have something that works.
My first thought for "How do I retrieve the number of children this comment has" is caching. Run a query to search whenever the parent comment is shown to get the number of children, then store that in the cache. When a new comment is added as a child, reset the cache for that parent.
WHY: because it means the count will always be correct and I don't need to think about things like comments being deleted and updating counts.
I can see that you might want to run queries against that number though, so if you're needing to get "Get me all the parent nodes who have more than X children" then running a sub-query for this might be slow, so storing the info might be better. In which case I would probably look to storing it on the comment itself 'comment_children_nodes' as a number. That would make it easy to look up in the datastore, you'd just need to keep that up-to-date each time a comment was added.
If that's the way you go, put a listener into the 'integrity check' that re-counts the counts when the integrity check is run so that if the counts do get out of sync with the actual comment count there is a way to get them back to correct.
Docs: "Events and Listeners"
https://www.jamroom.net/the-jamroom-network/documentation/jamroom-developers-guide/1011/events-and-listeners
maybe the 'repair_module' listener:
jrCore_register_event_listener('jrCore', 'repair_module', 'YOUR-MODULE_repair_module_listener');