John Davidson

php - Symfony Doctrine so many request

0 comments
Message:


In my API Symfony I have so many request generated by doctrine and I don't understand why.



My Controller Method:


/**
* @Route("/{id}/byteam", name="numero_numero")
*/
public function index(Race $race): Response
{
$grille=[];
$y=0;
$ligne=0;
$nbPilote = 600 + 20;
for ($i=0;$i<$nbPilote;$i++){
$grille[$ligne][$y]['num'] = $i+1;
$grille[$ligne][$y]['pilote'] = null;

$y++;
if ($y>=4){
$y=0;
$ligne++;
}
}

$listeTeam = [];
$listeTeam = $this->teamRepository->findAllByRace($race);
throw new \Exception('Stop');
$elite = new Team();
$elite->setName('Pilote Elite');
$ordre = [];
foreach ($this->inscriptionRepository->listeElite($race) as $inscription){
$elite->addInscription($inscription);
$ordre[] = $inscription->getId();
}
$elite->setOrdre($ordre);
array_unshift($listeTeam , $elite);


$sansTeam = new Team();
$sansTeam->setName('Sans Team');
$ordre = [];
foreach ($this->inscriptionRepository->listeInscritSansTeam($race) as $inscription){
$sansTeam->addInscription($inscription);
$ordre[] = $inscription->getId();
}
$sansTeam->setOrdre($ordre);
$listeTeam[] = $sansTeam;

return $this->render('numero/byteam.html.twig', [
'grille'=>$grille,
'listeTeam'=> $listeTeam
]);
}

My Repo Method:


public function findAllByRace(Race $race): ?array
{
return $this->createQueryBuilder('t')
->select('t,i,u')
->join('t.inscriptions','i')
->join('i.user','u')
->andWhere("t.race = :race")
->setParameter('race', $race)
->orderBy('t.name','ASC')
->getQuery()
->getResult()
;
}

With Repo Request (236 db calls):



Without Repo Request (23 db calls):



I have add throw for get Symfony debug bar.
I hope you understand me, my English is bad.



My Answer:
"/api/users", methods={"GET"})_ */ public function getUsers(UserRepository $userRepository) { $users = $userRepository->findAll(); return $this->json($users); }_This controller method is fetching all users from the database using the UserRepository class provided by Doctrine. If you are seeing a lot of requests being generated by this method, it could be due to a few reasons:_1. Lazy Loading: Doctrine uses lazy loading by default, which means that related entities are not loaded until they are accessed. If your User entity has relationships with other entities and those relationships are being accessed in your API response, it could be causing additional queries to be executed. You can use eager loading to fetch related entities along with the main entity to reduce the number of queries._2. N+1 Problem: If you are fetching a collection of entities and then iterating over them to access related entities, you may be running into the N+1 query problem. This happens when you make N+1 queries to fetch related entities for each entity in the collection. You can use join queries or batch fetching to optimize this._3. Query Optimization: Make sure that your database queries are optimized and that you are only fetching the data that you need. Avoid fetching unnecessary columns or entities to reduce the number of queries being executed._4. Caching: Consider implementing caching mechanisms to store the results of frequent queries and reduce the load on your database._By addressing these issues, you should be able to reduce the number of requests generated by Doctrine in your Symfony API.

Rate this post

3 of 5 based on 7200 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us