2
votes

I have run the query that returns quite a bit of data. In my controller I do

public function viewAction()
{
        $repository = $this
            ->getDoctrine()
            ->getManager()
            ->getRepository('NickAlertBundle:AvailabilityAlert');

        $alerts = $repository->getAllActiveAlerts();
        var_dump($alerts);

        return $this->render('NickAlertBundle:Page:view.html.twig', array(
            'alert' => $alerts,
        ));
}

That var_dump contains the data I need, it looks like

array (size=2)
  0 => 
    array (size=4)
      0 => 
        object(Nick\AlertBundle\Entity\AvailabilityAlert)[320]
          private 'id' => int 34
          private 'searchCommand' => string 'LONMEL' (length=12)
          private 'isConnecting' => string 'no' (length=2)
          private 'lastUpdated' => 
            object(DateTime)[323]
              ...
          private 'isDeleted' => boolean false
          private 'alertStatus' => string 'Active' (length=6)
      'classes' => string 'Business' (length=3)
      'flight_number' => string 'VS7' (length=3)
  1 => 
    ....

How would I go about getting this data displaying in my Twig file? I have tried

{{ alert.getSearchCommand() }}
{{ alert.searchCommand }}
{{ alert }}

But most of the time I get the following error:

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion")

So I was wondering how can I get this data displaying correctly?

1

1 Answers

4
votes

You need to loop over your array. You can do it with twig for :

In your case, something like this :

In controller :

I renamed your variable from alert to alerts as there can be more than one.

return $this->render('NickAlertBundle:Page:view.html.twig', array(
    'alerts' => $alerts,
));

In your twig file :

{% for alert in alerts %}
    //here you can access your entity
    {{ alert.searchCommand }}
{% endfor %}