JMS Serializer,Symfony2,是否可以将几个嵌套实体的JSON序列化为一个对象?

时间:2015-07-12 02:33:21

标签: symfony jmsserializerbundle

我有4个相关的实体。

套餐 - >有一个接收器 套餐 - >有PackageLines - > PackageLine引用了StockItem

是否可以序列化此JSON数据。

enter image description here

在包含所有其他实体的Package实体中?以干净有效的方式?

我已经尝试过使用JMS序列化程序包而没有太多运气。

库存商品是参考库存商品的ID。

封装

/**
 * Package
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="IREnterprise\AppBundle\Entity\PackageRepository")
 * @ORM\HasLifecycleCallbacks
 *
 * @ExclusionPolicy("all")
 *
 */
class Package
{

    CONST STATUS_COMPLETED = "COMPLETED";
    CONST STATUS_PROCESSING = "PROCESSING";
    CONST STATUS_CANCELLED = "CANCELLED";
    CONST STATUS_ON_HOLD = "ON_HOLD";
    CONST STATUS_FAILED = "FAILED";
    CONST STATUS_ERROR = "ERROR";


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

    /**
     * @ORM\ManyToOne(targetEntity="IREnterprise\UserBundle\Entity\User", inversedBy="packages")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     **/
    private $user;

    /**
     * @ORM\ManyToOne(targetEntity="IREnterprise\AppBundle\Entity\Receiver", inversedBy="package", cascade={"all"})
     * @Assert\Valid()
     * @Expose
     **/
    private $receiver;

    /**
     * @ORM\OneToMany(targetEntity="IREnterprise\AppBundle\Entity\PackageLine", mappedBy="package", cascade={"all"})
     * @Assert\Valid()
     * @Expose
     **/
    private $packageLines;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="createdAt", type="datetime")
     * @Expose
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updatedAt", type="datetime")
     * @Expose
     */
    private $updatedAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="shippedAt", type="datetime", nullable=true)
     * @Expose
     */
    private $shippedAt;

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

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


    public function __construct() {
        $this->setCreatedAt(new \DateTime('now'));
        $this->setPackageLines(new ArrayCollection());

        $this->setStatus(self::STATUS_PROCESSING);
    }

    ... getters & setters
}

接收者

/**
 * Receiver
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="IREnterprise\AppBundle\Entity\ReceiverRepository")
 *
 * @ExclusionPolicy("all")
 *
 */
class Receiver
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="IREnterprise\AppBundle\Entity\Package", mappedBy="receiver", cascade={"all"})
     **/
    private $packages;

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

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

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

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="zip_code", type="string", length=255)
     * @Assert\NotBlank()
     * @Expose
     */
    private $zipCode;

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=true)
     * @Assert\Email()
     * @Expose
     */
    private $email;

    public function __construct() {
        $this->setPackages(new ArrayCollection());
    }
}

PackageLine

/**
 * PackageLine
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="IREnterprise\AppBundle\Entity\PackageLineRepository")
 * @ORM\HasLifecycleCallbacks
 *
 * @ExclusionPolicy("all")
 *
 */
class PackageLine {

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

    /**
     * @ORM\ManyToOne(targetEntity="IREnterprise\AppBundle\Entity\Package", inversedBy="package", cascade={"all"})
     * @ORM\JoinColumn(name="package_id", referencedColumnName="id")
     **/
    private $package;

    /**
     * @ORM\ManyToOne(targetEntity="IREnterprise\AppBundle\Entity\StockItem", inversedBy="stockitem", cascade={"all"})
     * @ORM\JoinColumn(name="stockitem_id", referencedColumnName="id")
     * @Expose
     **/
    private $stockItem;

    /**
     * @var integer
     *
     * @ORM\Column(name="quantity", type="bigint")
     * @Expose
     */
    private $quantity;
}

StockItem

/**
 * StockItem
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="IREnterprise\AppBundle\Entity\StockItemRepository")
 * @ORM\HasLifecycleCallbacks
 *
 * @ExclusionPolicy("all")
 *
 */
class StockItem
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Expose
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="IREnterprise\UserBundle\Entity\User", inversedBy="stockItems")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     **/
    private $user;

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="SKU", type="string", length=255)
     * @Expose
     * @Assert\NotBlank()
     * @Assert\Type(type="digit", message="The value {{ value }} is not a valid {{ type }}.")
     */
    private $SKU;

    /**
     * @var integer
     *
     * @ORM\Column(name="stockCount", type="bigint")
     * @Expose
     */
    private $stockCount = 0;

    /**
     * @var integer
     *
     * @ORM\Column(name="orderCount", type="bigint")
     * @Expose
     */
    private $orderCount = 0;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="createdAt", type="datetime")
     * @Expose
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updatedAt", type="datetime")
     * @Expose
     */
    private $updatedAt;

    public function __construct() {
        $this->setCreatedAt(new \DateTime('now'));
    }
}

1 个答案:

答案 0 :(得分:-1)

很有可能。见例子

   class Package
    { 
        /**
         * @Type("string")
         */

       private $id;

        /**
         * @Type("ArrayCollection<MyNamespace\Recevier>")
         */
       private $receiver;
   }

   class Receiver
    {
      /**
       * @Type("string")
       */

        private $id;
     }

示例输出:

{  
   "package":{  
      "id":"1",
      "receiver":{  
         "id":"1"
      }
   }
}

请参阅文档 http://jmsyst.com/libs/serializer/master/reference/annotations 我认为这对你很有帮助