R
R
Rukis2019-01-19 18:47:06
Doctrine ORM
Rukis, 2019-01-19 18:47:06

Doctrine ORM does not recommend partial fetch, what are the alternatives?

If you limit the set of fields with a query, for example:

$em->createQuery("select u.id, u.name from MyApp\Domain\User u")->getResult();

then the output is not entities, but scalars.
The doc https://www.doctrine-project.org/projects/doctrine... explains why and suggests if you really need to use partial:
$em->createQuery("select partial u.{id, name} from MyApp\Domain\User u")->getResult();

we will receive entities in which only the specified fields are filled. It seems to be normal, but it doesn't work with the joyne anymore.
It is clear that getting only a part of the object in some cases is fraught with problems, but, for example, there is a typical task: display a list of posts indicating the author. We will need the Post id and title fields, and the User the name, post_id fields. Selecting everything is not very rational, because the field with the text of only one post is more voluminous than the fields of all records we need in total. This is a simplified example, the essence of the situation is that selecting all fields is not an option.
If we specify in the request a selection of only the required fields, then we will not receive objects, which means there is no point in ORM.
Does Doctrine offer a solution to this problem, or what is the general practice?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-01-19
@thewind

Is this not what you need? https://www.doctrine-project.org/projects/doctrine...

F
Flying, 2019-01-19
@Flying

Why doesn't it work with join? In theory, there should be no restrictions in this aspect.
If you do not need all the fields or you want to optimize the query so that it does not necessarily pull out all, then it is better to build a separate query and pull out only the necessary data as data, and not as entities. Ultimately, nothing can prevent you from making a request for data in your custom repository and creating entities filled with this data. In most cases, after all, the contents of one row of the database does not increase the size of the sample as much as the number of rows of the sample itself. Those. select, conditionally, 20 lines with the texts of posts will most likely be faster than 10k posts without their texts.
If it is so important for you to choose entities, but not to choose field values ​​(for example, each Post is a book for several megabytes of text, who knows?) - you can take the Post's text into a separate entity and use the `OneToOne` relation, then you will be able to take advantage of Doctrine's lazy loading of links and not select texts if they are not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question