简体   繁体   English

Symfony 2,使用Javascript存取实体对象

[英]Symfony 2, accees entities object in Javascript

it's my first post. 这是我的第一篇文章。 i create web application in Symfony 2 ( new for me ) 我在Symfony 2中创建了Web应用程序(对我来说是新的)

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

i send my entities to the Twig. 我将实体发送到树枝。

{{entities}} {{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) 这意味着用户1得分:2015/03/26为18(没关系)

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 我想用实体创建图表,当然我在统计中有多个数据,我只在StackOverflow中使用了一个

How can i access entities in Javascript ? 如何使用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. 我想将Google图表与实体一起使用,使用Google图表很容易,我可以随机输入数据,但是我需要访问实体以将其放入图表中。

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 http://symfony.com/doc/current/components/serializer.html

pass them to your twig after that as array of formated json. 然后将它们作为格式化的json数组传递给您的树枝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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