Getting Objects from Database in Symfony 2.7.3

I am trying to show some properties of objects stored in a database. I have a Controller, Entity and View. I am not making any exceptions, but I am not seeing the properties of the object.

Controller:

/**
* @Route ("/ov", name="ov")
*/
public function select(){

    $a=$this->getDoctrine()->getRepository('AppBundle:PC')->find(2);

    if(!$a){
        throw $this->createNotFoundExcepction('No PC');
    }

   return $this->render('PcDetailed.html.twig', array('pcs' => $a));

}

      

View:

{% extends 'master.html.twig' %}
{% block divCentral %}
    <div class="row">
        <p>Nom del pc</p>
        <div class="small-6 small-centered columns">
            {% for pc in pcs %}
                <p>{{ pc.nom }}</p>
            {% endfor %}
        </div>
    </div>
{% endblock %}

      

Edit:

Finally, as Chris says, the problem is that when using the View I am trying to iterate - it is an object, not an array. This is why it doesn't work.

The way I should do it:

return $this->render('PcDetailed.html.twig', array('pcs' => array($a)));

      

+3


source to share


1 answer


In your controller, you get a PC with id 2 and pass it to the view.

In the view, you are now trying to iterate over this object. I have no idea what TWIG does when you try to iterate over something that is not an array or collection, but maybe it just fails.



To fix this, change your controller code to send the array to the view:

return $this->render('PcDetailed.html.twig', array('pcs' => array($a)));

      

+1


source







All Articles