简体   繁体   中英

JMS Serializer dosen't format property

I making a RESTful app with Symfony2 and FOSRestBundle. FOSRestBundle uses JMS Seriazlizer to serialize data to json format. I have everything working with one little issue.

This is my Entity class

class Profil
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="secteur", type="string", length=255)
     */
    private $secteur;

    /**
     * @var string
     *
     * @ORM\Column(name="nomProfil", type="string", length=255, unique=true)
     */
    private $nomProfil;

    /**
     * @ORM\Column(name="actif", type="boolean")
     */
    private $actif;

    /**
     * @ORM\Column(name="dateFin", type="date")
     */
    private $dateFin;

    /**
     * @ORM\Column(name="orderNumberMTC", type="integer", length=100, nullable=true)
     */
    private $orderNumberMTC;

     /**
      * @Gedmo\Slug(fields={"nomProfil"})
      * @ORM\Column(length=128, unique=true)
      */
    private $slug;

    /**
     * @ORM\OneToOne(targetEntity="Genius\ProfileBundle\Entity\Societe", cascade={"persist", "remove"})
     */
    private $societe;

     /**
     * @ORM\OneToOne(targetEntity="Genius\ProfileBundle\Entity\Coordonnees", cascade={"persist", "remove"})
     */
    private $coordonnees;

    /**
     * @ORM\OneToMany(targetEntity="Genius\ProfileBundle\Entity\Professionnel", mappedBy="profil", cascade={"persist", "remove"})
     */
    private $professionnels;

     /**
     * @ORM\OneToMany(targetEntity="Genius\ProfileBundle\Entity\Lien", mappedBy="profil",cascade={"persist"}, cascade={"persist", "remove"})
     */
    private $liens;

    /**
     * @ORM\OneToMany(targetEntity="Genius\ProfileBundle\Entity\Album", mappedBy="profil",cascade={"persist"}, cascade={"persist", "remove"})
     */
    private $albums;

    /**
     * @ORM\OneToMany(targetEntity="Genius\ProfileBundle\Entity\Photo", mappedBy="profil",cascade={"persist"}, cascade={"persist", "remove"})
     */
    private $photos;

   /**    
    * @ORM\OneToMany(targetEntity="Genius\ProfileBundle\Entity\Actualite", mappedBy="profil",cascade={"persist"}, cascade={"persist", "remove"})    
    */
    private $actualites;

Controller Action :

 public function getActualitesAction(Request $request, ParamFetcherInterface $paramFetcher)
    {
        $offset = $paramFetcher->get('offset');
        $offset = null == $offset ? 0 : $offset;
        $limit = $paramFetcher->get('limit');


        return $this->container->get('genius_profile.actualite.handler')->all($limit, $offset);
        //var_dump($this->container->get('genius_profile.profil.handler')->all($limit, $offset)); die();

    }

genius_profile.actualite.handler IS an ORM

and this is json I get:

在此处输入图片说明

I have tried to force the property actualitie to array through the Annotation Type

Any Idea how can i force the property to Array ?

The Report Issue in github

You're probably filtering your collection somewhere or building it with custom keys, so array's keys are not in sequence (eg [0 => Object, 2 => Object] without 1 key) so it's serialized as object (representing associative array) instead of array.

You should create virtual property or custom accessor for this field and return collection as array_values(...) so keys will be sequenced properly and collection can be serialized as array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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