Answer the question
In order to leave comments, you need to log in
How to do selective pulling of entities in Doctrine2?
There are 2 tables:
Articles, Comments
Relationships:
Comments - ManyToOne - Articles
Articles - OneToMany - Comments
When selecting all articles, the doctrine accordingly gives me all the comments.
Question
Is it possible, when writing the query itself, to limit the entities that will be pulled up? (with one request, pull up comments with another not)
or do you need to create a separate repository for this, in which to describe the whole thing using DQL and only then call it?
Answer the question
In order to leave comments, you need to log in
Comments are pulled from the database at the moment when it is required:
$article = $articleRepository->find(1); // SELECT * FROM Articles WHERE id=1
$comments = $article->getComments(); // здесь не происходит запросов к БД
foreach ($comments as $comment) { // SELECT * FROM Comments WHERE articleId=1
echo $comment->getText();
}
$comment = $commentRepository->find(1); // SELECT * FROM Comments WHERE id=1
$article = $comment->getArticle(); // здесь не происходит запросов к БД
echo $article->getId(); // здесь тоже не происходит запросов к БД, вернет articleId комментария
echo $article->getName(); // SELECT * FROM Articles WHERE id=10
It is/should be by default
symfony.com/doc/current/book/doctrine.html#fetchin... (a note about Relationships and Proxy Classes )
There's an example
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product')
->find($id);
$category = $product->getCategory();
// prints "Proxies\AcmeStoreBundleEntityCategoryProxy"
echo get_class($category);
This proxy object extends the true Category object, and looks and acts exactly like it. The difference is that, by using a proxy object, Doctrine can delay querying for the real Category data until you actually need that data (eg until you call $category->getName()).
how do you do the fetching with dql? or just through find?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question