Converting a Symfony2 PHP Object Object for Use in Javascript

I am trying to open data from a PHP object (as shown below), but I would like to have access to this data in JavaScript for use in the graphics library.

The subject in question:

http://puu.sh/eP3QZ/e4289eb0d8.png

What I need to do is convert it to a JSON encoded object for use in Javascript.

I tried using twig in 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); 

The console shows this:

http://puu.sh/eP4aX/adf6c9978f.png

That's where I bang my head against the wall. Where can I access the values โ€‹โ€‹of these properties?

As an inefficient way, I managed to get it in 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 a little dirty for that. Should there be a better way to do this, of course?

Thanks in advance!

+6
source share
1 answer

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

You might want to write your own Twig extension to do this from 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 this line in your javascript, you should have a regular JS object where you can find whatever you want.

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


Update to give an overview of what is in the comments.

Both Symfony Serializer, like the JMSSerializerBundle, seem to have difficulty dealing with bidirectional relationships (most likely with the doctrine). This will lead to errors such as "Out of memory" or something regarding Self-referencing objects / infinite loops.

To solve this problem, you can ignore the attribute using Normalizer. Which would look something like this:

 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'); 
+8
source

Source: https://habr.com/ru/post/981346/


All Articles