转换 Symfony2 PHP 实体对象以在 Javascript 中使用 [英] Converting a Symfony2 PHP entity object for use within Javascript
问题描述
我正在尝试从 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.
有问题的对象:
我需要做的是将其转换为 JSON 编码的对象以在 Javascript 中使用.
What I need to be able to do is convert that into a JSON encoded object for use within Javascript.
我尝试在 Symfony 中使用 twig 来做到这一点:
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 }};
console.log(playerStats);
控制台显示:
这是我用头撞墙的地方.在哪里可以访问这些属性的值?
Which is where I'm banging my head against the wall. Where can I access the values of these properties?
作为一种低效方式,我设法通过以下方式将其放入 JavaScript 对象:
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?
提前致谢!
推荐答案
您可以使用 Symfony Serializer.http://symfony.com/doc/current/components/serializer.html
You can use the Symfony Serializer. http://symfony.com/doc/current/components/serializer.html
您可能想编写自己的 Twig 扩展来在模板中执行此操作.
You might want to write your own Twig extension to do this from within your template.
您的代码将如下所示:
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');
当你将该字符串放入你的 javascript 中时,你应该有一个普通的 JS 对象,你可以在其中找到你想要的一切.
When you put that string into your javascript, you should have a normal JS object where you can find everything you want.
您可能想要使用 JMSSerializerBundle,因为它已经有一个 Twig 扩展,而且通常更易于使用.
You might want to use the JMSSerializerBundle, as it already has a Twig extension, and is easier to use in general.
https://github.com/schmittjoh/JMSSerializerBundle/blob/master/Resources/doc/index.rst
快速概述评论内容的更新.
An update to give a quick overview of what is in the comments.
Symfony Serializer 和 JMSSerializerBundle 似乎都很难处理双向关系(很可能是 Doctrine).这将导致诸如内存不足"或提及自引用"对象/无限循环之类的错误.
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.
要解决这个问题,您可以使用 Normalizer 忽略该属性.看起来像:
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');
这篇关于转换 Symfony2 PHP 实体对象以在 Javascript 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!