title: Iterate or count ArrayCollection from twig
body: <p>symfony 2.8</p>
  
  <p>Ok, I have a Category entity which looks like this:</p>
  
  <pre><code>class Category
  {
  
      // ... //
      /**
       * @ORM\OneToMany(targetEntity="Classified", mappedBy="category")
       */
      private $classified;
  
      // ... // 
  
      public function __construct()
      {
          $this-&gt;classified = new ArrayCollection();
      }
  
  }
  </code></pre>
  
  <p>and the Classified entity looks like this:
  Class Classified</p>
  
  <pre><code>{
      /**
       * @ORM\ManyToOne(targetEntity="Category", inversedBy="classified")
       */
      private $category;
  }
  </code></pre>
  
  <p>In my result.html.twig I called to render the controller like this:</p>
  
  <pre><code>{{ render(controller('ClassifiedBundle:Category:displayCategoryPanelList'))}}
  </code></pre>
  
  <p>which basically calls Category Controller</p>
  
  <pre><code>class CategoryController extends Controller
  {
      public function displayCategoryPanelListAction(Request $request)
      {
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $categories = $em-&gt;getRepository('ClassifiedBundle:Category')
                      -&gt;findAllOrderedByName();
  
  
          return $this-&gt;render('ClassifiedBundle:Front/Widgets:category-panel-list.html.twig',
              array(
                  'categories'    =&gt; $categories
              ));
      }
  }
  </code></pre>
  
  <p>which in turn calls the CategoryRepository for query:</p>
  
  <pre><code>class CategoryRepository extends EntityRepository
  {
      public function findAllOrderedByName()
      {
  
          $query = $this-&gt;createQueryBuilder('c')
              -&gt;select('c, cc')
              -&gt;leftJoin('c.classified', 'cc')
              -&gt;orderBy('c.name', 'ASC')
              -&gt;getQuery()
              -&gt;getResult();
  
          return $query;
      }
  }
  </code></pre>
  
  <p>which in the end is rendered to category-panel-list.html.twig:</p>
  
  <pre><code>&lt;ul&gt;
    {% for c in categories %}
      &lt;li&gt;
        &lt;a href="#"&gt;
          &lt;i class="fa {{ c.icon }}"&gt;&lt;/i&gt;
          {{ c.name }} &lt;span class="category-counter"&gt;{# COUNT SHOULD GO HERE#}&lt;/span&gt;
        &lt;/a&gt;
      &lt;/li&gt;
    {% endfor %}
  
  &lt;/ul&gt;
  </code></pre>
  
  <p>Now, all these work fine apart from the counting classified arraycollection part.  I have tried <code>{{ c.classified|length }}</code> but this gives a fatal error.</p>
  
  <p>The dump looks good and I can see the entry for the classified collection, but just don't know how to count them.  Some categories have empty classified collection.
  <a href="https://i.stack.imgur.com/qWSts.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qWSts.png" alt="enter image description here"></a></p>