2012-08-02 8 views
6

Ho un problema con Symfony2 e Twig: non so come visualizzare tutti i campi della mia entità caricati dinamicamente. Ecco il mio codice (visualizza nulla !!)Symfony2 & Twig: visualizza tutti i campi e le chiavi

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 %} 

risposta

8

OK. Quello che stai cercando di fare non può essere fatto con un ciclo Twig for sul tuo oggetto attributi. Vorrei provare a spiegare:
Il ciclo Twig for esegue un'iterazione su un ARRAY di oggetti, eseguendo l'interno del ciclo per ciascuno degli oggetti nell'array. Nel tuo caso, $attributes NON è un array, è un OBJECT che hai recuperato con la tua chiamata findOneById. Quindi il ciclo for trova che questo non è un array e non esegue l'interno del ciclo, nemmeno una volta, ecco perché non si ottiene output.
La soluzione proposta da @thecatontheflat non funziona, poiché è la stessa iterazione su un array, solo che si ha accesso sia alle chiavi che ai valori dell'array, ma poiché $attributes non è un array, non viene eseguito nulla .

Quello che devi fare è passare al template un array con le proprietà dell'oggetto $ Attributes. Puoi usare la funzione php get_object_vars() per questo. Fare qualcosa di simile:

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

E nel template Twig:

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

tener conto che questo mostrerà solo le proprietà pubbliche del vostro oggetto.

+0

Buon punto @Carlos – Mick

+0

Grazie! Era il mio primo post e il mio inglese non è perfetto! Funziona ... – user1571729

-2

si dovrebbe cambiare per

{% for key, value in attributes %} 
    <p>{{ value }} : {{ key }}</p> 
{% endfor %} 
+0

E visualizzare nulla. Quando provo {{attributes.name}} o {{attributes.description}} tutto è ok. Ma dentro il ciclo niente! – user1571729

9

Non accontentarti solo delle proprietà pubbliche! Ottieni il privato/protetto pure!

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

} 
+1

L'utilizzo del componente [PropertyAccess] (http://symfony.com/doc/current/components/property_access/introduction.html) potrebbe essere più appropriato. – keyboardSmasher

0

Per Symfony3

$em = $this->getDoctrine()->getEntityManager(); 

    $MetaData = $em->getClassMetadata('TestBetaBundle:Node'); 
    $fields = $MetaData->getFieldNames(); 

    return $this->render('test/detail.html.twig', array('fields'=>fields));  
Problemi correlati