title: Symfony/Doctrine: SUM and AVG score of players
body: <p>I have in my database the tab: PLAYERS and a tab: SCORES.</p>
  
  <p>In tab SCORES i have these rows: ID - IDPLAYER - SCORE</p>
  
  <p>For example:</p>
  
  <pre><code>ID   IDPLAYER   SCORE
  ---------------------
  1       1         5
  2       2         4
  3       1         3
  4       2         1
  5       1         9
  </code></pre>
  
  <p>I want put in a template this:</p>
  
  <p>For "<strong>player 1</strong>" there are <strong>3</strong> scores.</p>
  
  <p>The <strong>count</strong> of the scores is "<strong>17</strong>" (9+3+5).</p>
  
  <p>The <strong>avg</strong> of the score of the player is "<strong>5.6</strong>" (17totscores / 3countScores).</p>
  
  <hr>
  
  <p>I have an entity with ORM, it' ok.</p>
  
  <p>I have a controller with this function:</p>
  
  <pre><code>public function avgScoreAction($id) {
  
  $queryScore = $this-&gt;getDoctrine()
  -&gt;getRepository('AcmeBundle:tabScores');
  
  $queryAvgScore = $queryScore-&gt;createQueryBuilder('g')
  -&gt;select("avg(g.score)")
  -&gt;where('g.idPlayer = :idPlayer')
  -&gt;setParameter('idPlayer', $id)
  -&gt;getQuery();
  
  $avgScore = $queryAvgScore-&gt;getResult();
  
  $result = ("Score average: ".$avgScore);
  
  return new Response($result);
  </code></pre>
  
  <p>But I have an error:</p>
  
  <pre><code>"Notice: Array to string conversion in this line:"
  
  $result = ("Score average: ".$avgScore);
  </code></pre>
  
  <p>If I write this:</p>
  
  <pre><code>$response = new Response();
  $response-&gt;setContent(json_encode(array($avgScore)));
  $response-&gt;headers-&gt;set('Content-Type', 'application/json');
  
  return $response;
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>[[{"1":"5.6667"}]]
  </code></pre>
  
  <p>which is the correct avg, but what is: [[{"1":" and "}]] ?????</p>