Mam takie encje:
users {id} matchdays {id} matches {id,matchday_id} 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:
namespace My\TyperkaBundle\Repository; use Doctrine\ORM\EntityRepository; class TypeRepository extends EntityRepository { public function getPointsPerMatchday(){ $qb = $this->createQueryBuilder('t'); $qb->select( 'SUM(t.numberOfPoints) AS suma' ,'u.username AS username' ,'u.id AS user' ,'u.priority' ,'md.id as matchday' ) ->innerJoin('t.match', 'm') ->innerJoin('m.matchday', 'md') ->innerJoin('t.user', 'u') ->where('md.id BETWEEN 1 AND 15') ->groupBy('u.username, md.id') ; $result = $qb->getQuery()->getResult(); // 1. Pobranie tylko użytkowników foreach ($result as $details){ $this->users[] = $details['user']; } // 2. Wybranie unikalnych wartości // 3. Uporządkowanie kluczy $this->users = array_combine($keys, $this->users); // 4. Zsumowanie punktów dla każdego użytkownika for($i=0;$i<10;$i++){ foreach ($result as $details){ if($this->users[$i] == $details['user']){ $this->sum_per_user[$this->users[$i]] = 0; } $this->sum_per_user[$this->users[$i]] = $this->sum_per_user[$this->users[$i]] + (int)$details['suma']; } } } // 5. Posortowanie użytkowników wg sumy punktów // 6. Kolejne uporządkowanie kluczy // 7. Uporządkowanie danych użytkowników wg sumy punktów ze wszystkich kolejek // oraz zmiana wyglądu tablicy na: dla każdego użytkownika sumy punktów w każdej kolejce foreach($this->users as $user){ foreach ($result as $details){ if($user == $details['user']){ $this->final_result[$details['username']][] = (int)$details['suma']; } } } } return $this->final_result; }
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.