T
T
tnz2013-08-02 06:13:13
Doctrine ORM
tnz, 2013-08-02 06:13:13

symfony. Doctrine. many-to-one. Select with one query, in short?

I am learning Symphony. I guess I don't know something, but
there are two tables, a Many-to-1 relationship. Well, let's say. posts and post authors You
need to get 10 posts and their authors.
We get all the posts - this is one request.
For each post, we get the author - this is for each post on request.
A total of 11 requests. And if you need to get data not from two tables, but more?
Another option is to manually collect the query in QueryBuilder, but it’s also a bit too much writing and you won’t be able to reuse it later. Bilder only get what it's worth.
You can make a separate method in the repository, something like findPostsJoinAuthors(...), you can already use the builder there, but anyway, if there are a lot of parameters, then the method will be greatly inflated. And I have more than two tables.
In Yii, this is easier. Registered (or generated by itself) links between models and

$post = Post::model()->with('author')->findAllByAttributes(array('category' => 10));<br>
$autor_name = $post->author->name;<br>

and this is one request.
Question: How is it customary to act in such cases and why so much writing for such simple things?
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Inori, 2013-08-02
@Inori

Correct answer:

“make a separate method in the repository, something like findPostsJoinAuthors(...), use the builder there”

Most likely the desired answer is to set fetch="EAGER" in the connection config like this .

M
MuXaJIbI4, 2013-08-02
@MuXaJIbI4

And why lay a bunch of magic methods in the kernel if they are rarely useful in their original form? Isn't it easier to register your method in the repository once and use it properly.

N
Nikita Gusakov, 2013-08-02
@hell0w0rd

Throw out the doctrine. Although she is universal, she is terribly not agile, and her code is smelly. If you look at the connections between objects, you will feel bad. All head objects inside refer to each other. And how the request is assembled ... At least once, completely go through the simplest selection in the debugger.
Any other ORM can easily be screwed to the symphony
. But if you still want to mess with the doctrine, either fetch = "EAGER", or in the repository with a qveri-builder, or whatever is more convenient for you)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question