Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF][SF2][Symfony2] Co tu jest nie tak ?, jak zoptymalizować ten kawałek kodu ?
damianooo
post
Post #1





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Chciałem się zapytać czy dobrze rozwiązałem poniższy problem:

Mam takie encje:

  1. users {id}
  2. matchdays {id}
  3. matches {id,matchday_id}
  4. points {id,user_id,match_id,numberOfpoints}


Potrzebuję pobrać dane tak aby wyświetlić dla każdego użytkownika sumy punktów dla każdej osobnej kolejki plus na końcu sumę wszystkich punktów ze wszystkich kolejek , tak aby później wyświetlić to w widoku TWIG, coś w tym stylu:

Imie /k1 /k2 /k3 /k4 /k5 / suma
---------------------------------
User3 / 4 / 2 / 4 / 2 / 4 / 16
User1 / 2 / 0 / 4 / 4 / 2 / 12
User2 / 2 / 0 / 2 / 4 / 2 / 1

Zrobiłem to tak:

  1. namespace My\TyperkaBundle\Repository;
  2. use Doctrine\ORM\EntityRepository;
  3.  
  4. class TypeRepository extends EntityRepository {
  5.  
  6. private $sum_per_user = array();
  7. private $users = array();
  8. private $final_result = array();
  9.  
  10. public function getPointsPerMatchday(){
  11.  
  12. $qb = $this->createQueryBuilder('t');
  13. $qb->select(
  14. 'SUM(t.numberOfPoints) AS suma'
  15. ,'u.username AS username'
  16. ,'u.id AS user'
  17. ,'u.priority'
  18. ,'md.id as matchday'
  19. )
  20. ->innerJoin('t.match', 'm')
  21. ->innerJoin('m.matchday', 'md')
  22. ->innerJoin('t.user', 'u')
  23. ->where('md.id BETWEEN 1 AND 15')
  24. ->groupBy('u.username, md.id')
  25. ;
  26.  
  27. $result = $qb->getQuery()->getResult();
  28.  
  29. // 1. Pobranie tylko użytkowników
  30. foreach ($result as $details){
  31. $this->users[] = $details['user'];
  32. }
  33.  
  34. // 2. Wybranie unikalnych wartości
  35. $this->users = array_unique($this->users);
  36.  
  37. // 3. Uporządkowanie kluczy
  38. $keys = array(0,1,2,3,4,5,6,7,8,9);
  39. $this->users = array_combine($keys, $this->users);
  40.  
  41. // 4. Zsumowanie punktów dla każdego użytkownika
  42. for($i=0;$i<10;$i++){
  43. foreach ($result as $details){
  44. if($this->users[$i] == $details['user']){
  45. if(!isset($this->sum_per_user[$this->users[$i]])){
  46. $this->sum_per_user[$this->users[$i]] = 0;
  47. }
  48. $this->sum_per_user[$this->users[$i]] = $this->sum_per_user[$this->users[$i]] + (int)$details['suma'];
  49. }
  50. }
  51. }
  52.  
  53. // 5. Posortowanie użytkowników wg sumy punktów
  54. arsort($this->sum_per_user);
  55.  
  56. // 6. Kolejne uporządkowanie kluczy
  57. $this->users = array_combine($keys, array_keys($this->sum_per_user));
  58.  
  59. // 7. Uporządkowanie danych użytkowników wg sumy punktów ze wszystkich kolejek
  60. // oraz zmiana wyglądu tablicy na: dla każdego użytkownika sumy punktów w każdej kolejce
  61. foreach($this->users as $user){
  62. foreach ($result as $details){
  63. if($user == $details['user']){
  64. if(isset($user)){
  65. $this->final_result[$details['username']][] = (int)$details['suma'];
  66. }
  67. }
  68. }
  69. }
  70.  
  71. return $this->final_result;
  72. }



Proszę o uwagi. Co zrobiłem źle ? Jak można to inaczej (może prościej) napisać ?
Będę wdzięczny za poprawki do kodu.

Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 20:26