Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 & Twig : display all fields and keys

I have a problem with Symfony2 and Twig: I don't know how to display all fields of my entity which is loaded dynamically. Here is my code (displays nothing!!)

Controller :

public function detailAction($id)
{
    $em = $this->container->get('doctrine')->getEntityManager();

    $node = 'testEntity'
    $Attributes = $em->getRepository('TestBetaBundle:'.$node)->findOneById($id);

    return $this->container->get('templating')->renderResponse('TestBetaBundle:test:detail.html.twig', 
    array(
    'attributes' => $Attributes
    ));

}

detail.html.twig :

    {% for key in attributes %} 
        <p>{{ value }} : {{ key }}</p>
    {% endfor %}
like image 952
user1571729 Avatar asked Aug 02 '12 14:08

user1571729


People also ask

What is Symfony used for?

Symfony is a feature-rich back-end framework that is used to build complex applications. Many developers still prefer the lightweight Silex micro-framework or the Symfony MicroKernel, and Symfony is a widely used application framework among open-source developers.

Which is better Symfony or Laravel?

There's no clear winner between Laravel and Symfony, as everything depends on your final goal. Laravel is a better choice if: This is your first experience with the framework, as it's easy to learn and has a simpler syntax and better learning materials.

What is Symphony software?

Symfony is an open-source PHP web application framework, designed for developers who need a simple and elegant toolkit to create full-featured web applications. Symfony is sponsored by SensioLabs. It was developed by Fabien Potencier in 2005.

Who created Symfony?

Symfony, powered by SensioLabs, was founded by Fabien Potencier in late 2004 to create websites faster. Shortly after its creation, he decided to release the software under the MIT license. Currently, it is one of the most popular PHP frameworks.


2 Answers

Don't settle for just the public properties! Get the private/protected as well!

public function detailAction($id){
    $em = $this->container->get('doctrine')->getEntityManager();

    $node = 'testEntity'
    $Attributes = $em->getRepository('TestBetaBundle:'.$node)->findOneById($id);

    // Must be a (FQCN) Fully Qualified ClassName !!!
    $MetaData = $em->getClassMetadata('Test\Beta\Bundle\Entity\'. $node);
    $fields = array();
    foreach ($MetaData->fieldNames as $value) {
        $fields[$value] = $Attributes->{'get'.ucfirst($value)}();
    }

    return $this->container
                ->get('templating')
                ->renderResponse('TestBetaBundle:test:detail.html.twig', 
                array(
                    'attributes' => $fields
                ));

}
like image 145
keyboardSmasher Avatar answered Sep 25 '22 11:09

keyboardSmasher


OK. What you are trying to do cannot be done with a Twig for loop over your attributes object. Let me try to explain:
The Twig for loop iterates over an ARRAY of objects, running the inside of the loop for each of the objects in the array. In your case, $attributes is NOT an array, it is an OBJECT which you retrived with your findOneById call. So the for loop finds that this is not an array and does not run the inside of the loop, not even once, that is why you get no output.
The solution proposed by @thecatontheflat does not work either, as it is just the same iteration over an array, only that you have access to both the keys and values of the array, but since $attributes is not an array, nothing is accomplished.

What you need to do is pass the template an array with the properties of the $Attributes object. You can use the php get_object_vars() function for this. Do something like:

$properties = get_object_vars ($Attributes);
return $this->container->get('templating')->renderResponse('TestBetaBundle:test:detail.html.twig', 
array(
'attributes' => $Attributes
'properties' => $properties
));

And in the Twig template:

{% for key, value in properties %} 
    <p>{{ value }} : {{ key }}</p>
{% endfor %}

Take into account that this will only show the public properties of your object.

like image 36
Carlos Granados Avatar answered Sep 23 '22 11:09

Carlos Granados