Fetching objects and adding a calculated field in Doctrine 2 I'm making a section that shows a list of blogs and how many comments it has without loading any comment data. My Blog entity is as follows:

/**
 * @ORM\Entity
 * @ORM\Table(name="blogs")
 */
class Blog
{   
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

   // This field is aggregated. Not in DB table
    protected $totalComments = 0;

    /**
     * @ORM\Column(type="string")
     */
    protected $title;

     /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="blog")
     */
    protected $comments;

    // More attributes

    public function __construct(){
        $this-&gt;comments = new ArrayCollection();
    }

    // Setters and Getters
}


and my Comment Entity:

class Comment
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Blog", inversedBy="comments")
     * @ORM\JoinColumn(name="blog_id", referencedColumnName="id")
     */
    protected $blog;

   // more attributes

    public function __construct(){     }

    // Setter and Getters
}


They have a oneToMany relationship. 1 Blog has N comments.

My next step is convert this query to the Doctrine2 World:

'SELECT b.*, COUNT( b.id ) AS totalComments FROM  `blogs` b LEFT JOIN comments c ON b.id = c.blog_id GROUP BY b.id'


That was an easy task:

function getLatestBlogs(){  
        $qb = $this-&gt;createQueryBuilder('b')
        -&gt;select('b, c')
        -&gt;addSelect('count(b.id) as totalComments')
        -&gt;leftJoin('b.comments', 'c')
        -&gt;groupBy('b.id');

        $result = $qb-&gt;getQuery()-&gt;getResult();

        foreach($qResult as $row){ //tweaking original result
            $row[0]-&gt;setTotalComments($row['totalComments']);
            $list[] = $row[0];
        }
        return $list;
    }


In the foreach loop is where I fix my fetched result because doctrine's getResult was returning an array of arrays like this:

// var_dump($result)
array (size=8)
  0 =&gt; 
    array(2) =&gt;
      0 =&gt;  
          object Blog
          'id' =&gt; int 330
          'title' =&gt; string 'title blog'
          // ... more attributes

      "totalComments" =&gt; int 5

  1 =&gt; ...
  );


Is there any fashion way that I'm missing ? I've really dig in to depth and couldn't find anything that fix this situation.
If you wanna just show them, just use 

count($blog-&gt;getComments()) or blog.comments|length in template, or something similar.