Symfony2 - 序列化具有关系的对象(ManyToMany,OneToMany ...)
Symfony2 - Serialize object with relations (ManyToMany, OneToMany ...)
谁能告诉我是否可以序列化具有关系的实体?
json_encode 已经有效,但我的对象看起来像这样:
{
"id": 1,
"lot": 32,
"num": "533987",
"date_modification": {
"date": "2015-02-17 14:24:52",
"timezone_type": 3,
"timezone": "Europe/Paris"
},
"customer": {
"id": 1,
"lastname": "DUFRESNE",
"firstname": "CHRISTOPHE",
}
}
但我想用子对象序列化数据。事实上是这样的:
{
"id": 1,
"lot": 32,
"num": "533987",
"date_modification": "2015-02-17",
"customer": "DUFRESNE CHRISTOPHE",
}
}
所以我查看了文档:http://symfony.com/doc/current/components/serializer.html
但我看不出是否可以处理 ManyToMany 等关系以及如何操作?
这是我的实体:
/**
* Subscription (BS)
*
* @ORM\Table(name="subscription")
* @ORM\Entity(repositoryClass="Jcd\LiteyearBundle\Entity\SubscriptionRepository")
* @ORM\HasLifecycleCallbacks
*/
class Subscription {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="lot", type="smallint")
*/
private $lot;
/**
* @var string
* @ORM\Column(name="num", type="string", length=9)
*/
private $num;
/**
* @var \DateTime
* @ORM\Column(name="date_creation", type="datetime", nullable=true)
*/
private $date_creation;
/**
* @var \DateTime
* @ORM\Column(name="date_modification", type="datetime", nullable=true)
*/
private $date_modification;
/**
* @var \DateTime
* @ORM\Column(name="date_completeness", type="datetime", nullable=true)
*/
private $date_completeness;
/**
* @var \DateTime
* @ORM\Column(name="date_reception", type="datetime", nullable=true)
*/
private $date_reception;
/**
* @var \DateTime
* @ORM\Column(name="date_signature", type="datetime", nullable=true)
*/
private $date_signature;
/**
* @var \DateTime
* @ORM\Column(name="date_entry", type="datetime", nullable=true)
*/
private $date_entry;
/**
* @var \Integer
* @ORM\Column(name="statement", type="integer", nullable=true)
*/
private $statement; // Releve du compteur
/**
* @var \Integer
* @ORM\Column(name="statement_kind", type="integer", nullable=true)
*/
private $statement_kind; // Personne qui a relevé le compteur
/**
* @var \Integer
* @ORM\Column(name="car", type="integer", nullable=true)
*/
private $car; // Consommation annuelle de reference
/**
* @var boolean
* @ORM\Column(name="pro", type="boolean", options={"default" = false}, nullable=true)
*/
private $pro;
/**
* @ORM\ManyToOne(targetEntity="Jcd\LiteyearBundle\Entity\User", cascade={"persist"}, inversedBy="adv_subscriptions")
* @ORM\JoinColumn(name="adv_id", referencedColumnName="id", nullable=true)
*/
private $adv;
/**
* @ORM\ManyToOne(targetEntity="Jcd\LiteyearBundle\Entity\User", cascade={"persist"}, inversedBy="vendor_subscriptions")
* @ORM\JoinColumn(name="vendor_id", referencedColumnName="id", nullable=true)
*/
private $vendor;
/**
* @var \Integer
* @ORM\Column(name="provider", type="integer")
*/
private $provider;
/**
* @var \Integer
* @ORM\Column(name="state", type="integer")
*/
private $state;
/**
* @var \Integer
* @ORM\Column(name="payment", type="integer")
*/
private $payment;
/**
* @var \Integer
* @ORM\Column(name="billing", type="integer")
*/
private $billing;
/**
* @var string
* @ORM\Column(name="comment", type="text", nullable=true)
*/
private $comment;
/**
* @var float
* @ORM\Column(name="wage", type="float", nullable=true)
*/
private $wage; // Anciennement Rem
/**
* @var float
* @ORM\Column(name="com", type="float", nullable=true)
*/
private $com; // Anciennement Commission
/**
* @var string
* @ORM\Column(name="offer", type="text", nullable=true)
*/
private $offer; // Anciennement Prix fixe
/**
* @var \Integer
* @ORM\Column(name="mail_kind", type="integer", nullable=true)
*/
private $mail_kind; //Anciennement Mail
/**
* @ORM\OneToMany(targetEntity="SubscriptionTrack", cascade={"persist", "remove"}, mappedBy="subscription")
*/
private $tracks;
/**
* @ORM\ManyToMany(targetEntity="SubscriptionSalary", cascade={"persist"}, mappedBy="subscriptions")
**/
private $salaries;
/**
* @ORM\OneToOne(targetEntity="Jcd\LiteyearBundle\Entity\Customer")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
**/
private $customer;
谢谢您的回答!
实现您想要的效果的最简单方法是使用 JMSSerializerBundle
,它具有许多您可能想要使用的功能。
谁能告诉我是否可以序列化具有关系的实体? json_encode 已经有效,但我的对象看起来像这样:
{
"id": 1,
"lot": 32,
"num": "533987",
"date_modification": {
"date": "2015-02-17 14:24:52",
"timezone_type": 3,
"timezone": "Europe/Paris"
},
"customer": {
"id": 1,
"lastname": "DUFRESNE",
"firstname": "CHRISTOPHE",
}
}
但我想用子对象序列化数据。事实上是这样的:
{
"id": 1,
"lot": 32,
"num": "533987",
"date_modification": "2015-02-17",
"customer": "DUFRESNE CHRISTOPHE",
}
}
所以我查看了文档:http://symfony.com/doc/current/components/serializer.html 但我看不出是否可以处理 ManyToMany 等关系以及如何操作?
这是我的实体:
/**
* Subscription (BS)
*
* @ORM\Table(name="subscription")
* @ORM\Entity(repositoryClass="Jcd\LiteyearBundle\Entity\SubscriptionRepository")
* @ORM\HasLifecycleCallbacks
*/
class Subscription {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="lot", type="smallint")
*/
private $lot;
/**
* @var string
* @ORM\Column(name="num", type="string", length=9)
*/
private $num;
/**
* @var \DateTime
* @ORM\Column(name="date_creation", type="datetime", nullable=true)
*/
private $date_creation;
/**
* @var \DateTime
* @ORM\Column(name="date_modification", type="datetime", nullable=true)
*/
private $date_modification;
/**
* @var \DateTime
* @ORM\Column(name="date_completeness", type="datetime", nullable=true)
*/
private $date_completeness;
/**
* @var \DateTime
* @ORM\Column(name="date_reception", type="datetime", nullable=true)
*/
private $date_reception;
/**
* @var \DateTime
* @ORM\Column(name="date_signature", type="datetime", nullable=true)
*/
private $date_signature;
/**
* @var \DateTime
* @ORM\Column(name="date_entry", type="datetime", nullable=true)
*/
private $date_entry;
/**
* @var \Integer
* @ORM\Column(name="statement", type="integer", nullable=true)
*/
private $statement; // Releve du compteur
/**
* @var \Integer
* @ORM\Column(name="statement_kind", type="integer", nullable=true)
*/
private $statement_kind; // Personne qui a relevé le compteur
/**
* @var \Integer
* @ORM\Column(name="car", type="integer", nullable=true)
*/
private $car; // Consommation annuelle de reference
/**
* @var boolean
* @ORM\Column(name="pro", type="boolean", options={"default" = false}, nullable=true)
*/
private $pro;
/**
* @ORM\ManyToOne(targetEntity="Jcd\LiteyearBundle\Entity\User", cascade={"persist"}, inversedBy="adv_subscriptions")
* @ORM\JoinColumn(name="adv_id", referencedColumnName="id", nullable=true)
*/
private $adv;
/**
* @ORM\ManyToOne(targetEntity="Jcd\LiteyearBundle\Entity\User", cascade={"persist"}, inversedBy="vendor_subscriptions")
* @ORM\JoinColumn(name="vendor_id", referencedColumnName="id", nullable=true)
*/
private $vendor;
/**
* @var \Integer
* @ORM\Column(name="provider", type="integer")
*/
private $provider;
/**
* @var \Integer
* @ORM\Column(name="state", type="integer")
*/
private $state;
/**
* @var \Integer
* @ORM\Column(name="payment", type="integer")
*/
private $payment;
/**
* @var \Integer
* @ORM\Column(name="billing", type="integer")
*/
private $billing;
/**
* @var string
* @ORM\Column(name="comment", type="text", nullable=true)
*/
private $comment;
/**
* @var float
* @ORM\Column(name="wage", type="float", nullable=true)
*/
private $wage; // Anciennement Rem
/**
* @var float
* @ORM\Column(name="com", type="float", nullable=true)
*/
private $com; // Anciennement Commission
/**
* @var string
* @ORM\Column(name="offer", type="text", nullable=true)
*/
private $offer; // Anciennement Prix fixe
/**
* @var \Integer
* @ORM\Column(name="mail_kind", type="integer", nullable=true)
*/
private $mail_kind; //Anciennement Mail
/**
* @ORM\OneToMany(targetEntity="SubscriptionTrack", cascade={"persist", "remove"}, mappedBy="subscription")
*/
private $tracks;
/**
* @ORM\ManyToMany(targetEntity="SubscriptionSalary", cascade={"persist"}, mappedBy="subscriptions")
**/
private $salaries;
/**
* @ORM\OneToOne(targetEntity="Jcd\LiteyearBundle\Entity\Customer")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
**/
private $customer;
谢谢您的回答!
实现您想要的效果的最简单方法是使用 JMSSerializerBundle
,它具有许多您可能想要使用的功能。