2014-01-08 75 views
3

我有一個實體(項目),其具有多對一關係到另一個實體(型)序列化實體宣稱這裏:與協會

 /** 
    * @ORM\ManyToOne(targetEntity="Type", inversedBy="item") 
    * @ORM\JoinColumn(name="type_id", referencedColumnName="id", nullable=true) 
    * @Expose 
    */ 
    protected $itemType; 

當我序列化的項目,它包括對所引用的其他屬性實體(類型)當所有我想要的是隻是ID:

item_type: { 
    id: 1 
}, 

,而不是與此:

item_type: { 
    id: 1, 
    name: "Case & Cover", 
    description: "Keep your phone safe with stylish cases and covers" 
}, 

我已閱讀文檔並使用組和maxdepths進行測試,但沒有運氣,並瀏覽了我遇到的這些問題: https://github.com/schmittjoh/JMSSerializerBundle/issues/61#issuecomment-3297955

此功能已包括在內嗎?

使用"jms/serializer-bundle": "dev-master"

編輯

這是協會(S)樣品序列化實體:

{ 
    id: 1, 
    name: "iPhone 5c Slim Genuine Leather Portfolio Case with Stand - Classic Black", 
    price: 29.95, 
    description: "...", 
    image: "1.jpg", 
    item_type: { 
     id: 1, 
     name: "Case & Cover" 
    }, 
    item_brand: [ 
     { 
      id: 1, 
      name: "Apple" 
     } 
    ] 
} 

我真正想要的是這個(沒有相關的任何額外的屬性實體):(使用JMS串行器,因爲它是一個很棒的庫:))

{ 
    id: 1, 
    name: "iPhone 5c Slim Genuine Leather Portfolio Case with Stand - Classic Black", 
    price: 29.95, 
    description: "...", 
    image: "1.jpg", 
    item_type: { 
     id: 1 
    }, 
    item_brand: [ 
     { 
      id: 1 
     } 
    ] 
} 

回答

1

您必須將exludePolicy用於實體Type@ExclusionPolicy('all')並將@Expose添加到id屬性。

看到的文檔http://jmsyst.com/libs/serializer/master/reference/annotations

希望它有助於

最好的方面。

+0

這就是我所做的,實際上我在課前加入了@ExclusionPolicy('all')'。但我需要另一個控制器上的其他屬性,但只是沒有任何關聯的(Type)實體,我需要排除實體上的其他屬性,除id之外的關聯 – user1076813