简体   繁体   中英

Symfony 2, accees entities object in Javascript

it's my first post. i create web application in Symfony 2 ( new for me )

Class Statistique
 id         //int
 userId     //int
 ladate     //date
 scores     //int

i send my entities to the Twig.

{{entities}}

array (size=1)
0 => 
object(Did\MembresBundle\Entity\Statistique)[420]
private 'id' => int 1
private 'userId' => int 1
private 'ladate' => 
object(DateTime)[418]
public 'date' => string '2015-03-26 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)
private 'scores' => int 18

it's mean Users 1 make scores:18 the 2015/03/26 ( It's OK)

I would like to create chart with entities of course I have more than 1 data in statistique i put only one for use in StackOverflow

How can i access entities in Javascript ?

I would like to use Google Chart with entities, it's easy to use Google Chart, i put random data it's ok, but i need to access entities, to put it in chart.

i tried this

<script type="text/javascript">
    var tabstat = {{entities|json_encode|raw}};        
    alert(tabstat[0].scores); // give me Undefined
    alert(tabstat);//give me [object Object ]
</script>

what's wrong with this ?

copy past code

<script type="text/javascript">

    var tabstat = [{}];        
    alert(tabstat[0]);
    alert(tabstat);
</script>

You have to serialize your entities first in your controller :

http://symfony.com/doc/current/components/serializer.html

pass them to your twig after that as array of formated json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM