I am new to symfony so sorry if this is something really simple to anwer.
For the sake of example I have rewritten code snippets as if I was writing a blog.
I have a BlogPost
entity with collection of BlogComment
s annotated like this:
/**
* @OneToMany(targetEntity="BlogComment", mappedBy="post")
*/
private $comments;
From my amateur point of view, Doctrine likes to work with complete objects, so this collection is either not initialized, or lazily loaded whenever I use the reference to it.
I guess you can imagine the overhead and memory requirements when every one of my BlogPost
s has at least 500 Blogcomment
s and they all get initialized whenever I touch $comments
variable.
What I am trying to achieve is to list ie. 50 blog posts, each with 20 latest comments (without the memory explosion). Additionally, I would like to be able to display only top 20 comments with most "likes" (or generally just select a subset based on some criteria).
Is there any generally recommended and clean way to achieve this kind of functionality? And when I achieve this, isn't use of such "incomplete" or "modified" entities going to break my logic (when updating/deleting items from the subset and persisting it)? I assume that solution to this will likely be a method in a custom repository, but I still can't see the thought behind it.
In advance, thank you for answers, I am really curious what kinds of solutions you will be able to come up with.