1

I’m developing an application that inherits abstract classes. These abstract classes have their own mapping for the serializer as shown in the example bellow

Hezten\CoreBundle\Model\Enroled:
    exclusion_policy: ALL

And the abstract class:

<?php

namespace Hezten\CoreBundle\Model;

abstract class Enroled implements EnroledInterface
{
    protected $student;

    protected $subject;

    //Some functions...

}

The class that inherits the previous class looks as follows

<?php

namespace XXX\XXXBundle\Entity;

use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Exclude;

use Doctrine\ORM\Mapping as ORM;

use Hezten\CoreBundle\Model\Enroled as BaseEnroled;

/** 
 * @ORM\Entity 
 * @ExclusionPolicy("NONE")
 */
class Enroled extends BaseEnroled
{
    /** @ORM\Id  
     *  @ORM\ManyToOne(targetEntity="XXX\XXXBundle\Entity\Student", inversedBy="enroled")
     * @Exclude
     */
    protected $student;

    /** @ORM\Id  
     *  @ORM\ManyToOne(targetEntity="XXX\XXXBundle\Entity\Subject", inversedBy="enroled")
     *  @Exclude
     */
    protected $subject;

    /** @ORM\Column(type="boolean") */
    private $field0;

    /** @ORM\Column(type="boolean") 
     */
    private $field1;

    /** @ORM\Column(type="boolean") */
    private $field2;
 }

The error thrown says this:

Warning: json_encode() [<a href='function.json-encode'>function.json-encode</a>]: recursion detected in C:\xampp\htdocs\Project\vendor\jms\serializer\src\JMS\Serializer\JsonSerializationVisitor.php line 29

For sure, I'm doing something wrong as no entities are exposed, just three fields of "Enroled" entity according to the mappings but I have no clue. I spent a couple of days trying to figure out what's the mistake without success.

What is the proper way to do the mapping of inhertied properties?

Update

Code used to serialize JSON using FOSRestBundle:

    $students = $this->get('hezten_core.manager.enroled')->findEnroledBySubject($subject);

    $view = View::create()
    ->setStatusCode(200)
    ->setFormat('json')
    ->setData($students);

    return $this->get('fos_rest.view_handler')->handle($view);
Gorka Lauzirika
  • 303
  • 5
  • 18
  • Have a look at http://stackoverflow.com/questions/6706485/how-to-encode-doctrine-entities-to-json-in-symfony-2-0-ajax-application ... and ... http://stackoverflow.com/questions/11851197/avoiding-recursion-with-doctrine-entities-and-jmsserializer ... Can you provide the code you use to serialize to JSON ? – Nicolai Fröhlich Jul 17 '13 at 18:52
  • Try setting `@ExclusionPolicy` to `"none"` instead of `"NONE"` to match the documentation values... but i don't think that's it. http://jmsyst.com/libs/serializer/master/reference/annotations#exclusionpolicy – Nicolai Fröhlich Jul 17 '13 at 19:00
  • How exactly shall your serialized JSON output look? – Nicolai Fröhlich Jul 17 '13 at 19:08
  • @nifr Thanks for the help but the lowercases don't work. I guess the problem is related to the inheritance as I have another version working with the same serializing code but without using inherintance for the entities. – Gorka Lauzirika Jul 17 '13 at 21:17
  • I'm not sure what you're trying to achieve ... do you want to have student and subject properties in your xml or not? ... just to get it right ... you say the other properties are included in the json output ... you're excluding student and subject explicitely ... twice ... is that what you want? When is the exception exactly thrown? if you add the mapping to your extending entity? – Nicolai Fröhlich Jul 18 '13 at 01:07
  • @nifr Well... I'm trying to avoid the recursion error removing ManyToOne fields using '@Exclude'. In the final version, subjects and students will be displayed in the json. My question is related more about how to implement serialization when inheriting from other classes rather than finding the correct way to implement this exact example as I have a working example of this api call but without inheriting from a parent class. Thanks! – Gorka Lauzirika Jul 18 '13 at 09:38
  • If I remove the inheritance from the `Enroled` class the serializer works as expected... I'm missing something... – Gorka Lauzirika Jul 18 '13 at 10:02

1 Answers1

0

Finally the API is working... I had to override the metadata of the classes I was inheriting adding the following lines to the config.yml

jms_serializer:
    metadata:
        directories:
            HeztenCoreBundle:
                namespace_prefix: "Hezten\\CoreBundle"
                path: "%kernel.root_dir%/serializer/HeztenCoreBundle"

In the path that is selected above I added one yml file for each Model setting exclusion policy to ALL:

Hezten\CoreBundle\Model\Enroled:
    exclusion_policy: ALL

And I used annotations on the entities that were inheriting those models to expose required info.

I don't know if this is the best approach but works well for me

Gorka Lauzirika
  • 303
  • 5
  • 18