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)));

1 Answer 1

1

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

1 Comment

You're right, I thought the iteration doesn't affect me. Thank you.

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.