[英]Doctrine ORM object type column not being serialized
我正在使用REST api,並且有一列類型的object作為我的User類的屬性。 在我的用戶實體中,我有:
/**
* @var stdClass
* @Serializer\Expose
* @Serializer\Groups({"list", "details"})
* @ORM\Column(type="object", name="notifications")
*/
protected $notifications;
創建新用戶時,我使用不同通知的默認值初始化此字段,如下所示:
$_notifications = new \stdClass();
$_notifications->voucherSold = true;
$_notifications->voucherRedeemed = true;
$_notifications->newConnection = true;
$user->setNotifications($_notifications);
我可以看到此字段已正確寫入數據庫。 在我的數據庫中,在notifications
列下,我得到:
O:8:"stdClass":3:{s:11:"voucherSold";b:1;s:15:"voucherRedeemed";b:1;s:13:"newConnection";b:1;}
但是,當我通過API加載該用戶時,我在通知字段中最終得到一個空對象:
{
"resource": "vendors/39",
"id": 39,
"email": "test@user.com",
"notifications": {},
"company": "",
"address": "",
"city": "",
"state": "",
"zipcode": "",
"phone": "",
"website": ""
}
我不知道為什么沒有實現價值。 有任何想法嗎? 謝謝。
json編碼器只能將數組轉換為json,並且只會將對象設置為對象{}
而不是在其中尋找字段等。
嘗試將$_notifications
對象轉換為數組,然后返回:
$user->setNotifications((array) $_notifications);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.