N
N
Nikolai Egorov2018-10-29 12:49:58
symfony
Nikolai Egorov, 2018-10-29 12:49:58

Where and when should you use partial doctrine objects?

Hello!
In the process of studying the Symphony, it became necessary to load 40-50 objects from the database with 5-6 kb of text. These objects are loaded on every page of the site - categories and tags. Yes, each tag and category has text up to 5-6kb, but this text is displayed only for the active category/tag - the rest, respectively, simply load the server. I made a separate method in the repository that would not return all the category data, but only the necessary ones:

$query = $qb
            ->select('c.id, с.title, с.slug')
            ->getQuery();
        return $query->getResult();

As a result, I got an array, not objects of the Category type - so I found out that in order to get a partially loaded object, you need to use the Doctrine mechanism for partial loading of objects, which has a peculiarity - if you then request an already loaded PARTIAL object, then the doctrine will return it , but not complete.
It turns out that two methods can be used to increase performance:
- simple arrays
- and Partial objects.
In the admin panel, in the project that I'm doing now, I don't see any reason to worry too much about performance yet - there is no such traffic there, and there is no list of categories on each page. So there, I will use fully loaded objects.
But with the public part, I want to think about performance and just understand the topic - what is better to use in such cases: arrays, or partial objects?
On the one hand, using arrays is a simple and quick solution for displaying the same list of categories on every page of the site. But on the other hand, these are not objects already, and it turns out that in different parts of the project there can be different data types of the same entity - somewhere objects, but here and there arrays.
Prompt, please:
1. When do you use Partial objects?
2. Do you use arrays and in what cases?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
voronkovich, 2018-10-29
@nickicool

In my opinion it is better not to use them at all.
1. This idea was taken from Hibernate. But, Java allows you to subsequently reload data that was not loaded initially. Those. using partial objects cannot result in an error. In PHP, objects do not have an API similar to Java. I have a suspicion that this is why partial objects will be removed from Doctrine 3, as they did with support for detach / merge operations.
2. The implementation of partial objects in Doctrine contradicts the principle "A model must always be valid"
By the way, you forgot about the option of using a lightweight DTO (see the Flyweight pattern), instead of arrays. See https://www.doctrine-project.org/projects/doctrine... Arrays are easy to serialize, for example, you can quickly make an API with them:
When can arrays be used?

public function api(EntityManagerInterface $entityManager): Response
{
    $posts = $entityManager->createQuery('SELECT p FROM App\Entity\Post p')->getArrayResult();

    return new JsonResponse($posts);
}

D
DevMan, 2015-09-06
@ddshadoww

it also writes in English and white: "Permission denied"
try:sudo pip install -U grab

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question