[英]JMS Serializer dosen't format property
I making a RESTful app with Symfony2 and FOSRestBundle. 我使用Symfony2和FOSRestBundle制作了一个RESTful应用程序。 FOSRestBundle uses JMS Seriazlizer to serialize data to json format.
FOSRestBundle使用JMS Seriazlizer将数据序列化为json格式。 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 genius_profile.actualite.handler是一个ORM
and this is json I get: 这是我得到的json:
I have tried to force the property actualitie
to array through the Annotation Type 我试图财产强制
actualitie
通过数组中的注释类型
Any Idea how can i force the property to Array ? 任何想法,我怎么能强制该属性为数组?
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. 您可能在某个地方过滤集合或使用自定义键构建集合,因此数组的键不是按顺序排列的(例如
[0 => Object, 2 => Object]
没有1
键),因此它被序列化为对象(表示关联数组)的数组。
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. 您应该为此字段创建虚拟属性或自定义访问器,并以
array_values(...)
返回collection,以便键可以正确排序,并且collection可以序列化为array。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.