jcobo1
jcobo1

Reputation: 1180

Fetching objects from database in Symfony 2.7.3

I'm trying to show some object properties stored on the database. I've got the controller, the Entity, and the view. I'get no excepctions but I can't see the object properties.

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, like Chris says, the problem is 'cause on the View I'm using I'm trying to iterate is an object, not an array. That's why doesn't work.

That's the way I must do it:

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

Upvotes: 3

Views: 464

Answers (1)

Chris
Chris

Reputation: 7184

In your controller you get the 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 a collection but maybe it just fails silently.

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

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

Upvotes: 1

Related Questions