Symfony2 - 使用关系序列化对象(ManyToMany,OneToMany ......)

时间:2015-02-18 10:09:43

标签: php json symfony serialization entity

有人可以告诉我是否可以序列化有关系的实体? 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",
  }
}

但我想用withotu子对象序列化数据。事实上是这样的:

{
  "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;

谢谢你的回答!

1 个答案:

答案 0 :(得分:5)

实现目标的最简单方法是使用具有许多功能的JMSSerializerBundle

试一试:http://jmsyst.com/bundles/JMSSerializerBundle