dqq22391 2015-01-21 13:21
浏览 59

转换Symfony2 PHP实体对象以在Javascript中使用

I'm attempting to open up data from a PHP Object (as shown below) but I'd like to be able to access this data within JavaScript to use in a graphing library.

Object in question:


What I need to be able to do is convert that into a JSON encoded object for use within Javascript.

I tried using twig within Symfony to do this via:

{% set playerStats = match.getStatsPlayers().getValues() }%
{% dump(playerStats) %} // This is what you see above

var playerStats = {{ playerStats|json_encode }};


The console shows this:


Which is where I'm banging my head against the wall. Where can I access the values of these properties?

As an inefficient way, I've managed to get it into a JavaScript object by doing:

{% for p in playerStats %}
    playerStats.push({ 'id': {{p.playerID}}, 'playerName': '{{p.playerName}}', 'playerOutfit': {{p.playerOutfit}}, 'playerFaction': {{p.playerFaction}}, 'playerKills': {{p.playerKills}}, 'playerDeaths': {{p.playerDeaths}}, 'playerTeamKills': {{p.playerTeamKills}}, 'playerSuicides': {{p.playerSuicides}} });
{% endfor %}

I feel kinda dirty for doing that. There must be a better way to do this surely?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • drq1257 2015-01-21 13:32

    You can use the Symfony Serializer. http://symfony.com/doc/current/components/serializer.html

    You might want to write your own Twig extension to do this from within your template.

    Your code will look something like this:

    use Symfony\Component\Serializer\Serializer;
    use Symfony\Component\Serializer\Encoder\JsonEncoder;
    use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
    $encoders = array(new JsonEncoder());
    $normalizers = array(new GetSetMethodNormalizer());
    $serializer = new Serializer($normalizers, $encoders);
    $jsonContent = $serializer->serialize($object, 'json');

    When you put that string into your javascript, you should have a normal JS object where you can find everything you want.

    You might want to use the JMSSerializerBundle, as it already has a Twig extension, and is easier to use in general.


    An update to give a quick overview of what is in the comments.

    Both the Symfony Serializer as the JMSSerializerBundle seem to have a hard time coping with Bi-directional relations (most likely Doctrine). This will result in errors like "Out of memory" or something mentioning "Self-referencing" objects / infinite loops.

    To solve this, you can ignore the attribute with the Normalizer. Which will look something like:

    use Symfony\Component\Serializer\Serializer;
    use Symfony\Component\Serializer\Encoder\JsonEncoder;
    use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
    $normalizer = new GetSetMethodNormalizer();
    $normalizer->setIgnoredAttributes(array('match')); //Replace match with the parent attribute
    $encoder = new JsonEncoder();
    $serializer = new Serializer(array($normalizer), array($encoder));
    $serializer->serialize($object, 'json');
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 做一个web书店,带答辩
  • ¥15 #求带 python,使用pycharm爬取智联招聘数据资源并保存为Excel文件,同时以图表形式呈现
  • ¥15 学校优化算法怎么样,国内用的多吗?
  • ¥15 微信购物商城购物车的所有功能以及购物车,和首页的搜索功能
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决