Answer the question
In order to leave comments, you need to log in
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();
Answer the question
In order to leave comments, you need to log in
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);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question