2

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 Answer 1

4

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 %}
Sign up to request clarification or add additional context in comments.

1 Comment

I did attempt that, but I still have a problem with it complaining about array keys. I think it may be because I have a multidimensional array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.