Php 未序列化的ORM对象类型列
我正在开发一个RESTAPI,我有一个object类型的列作为我的用户类的属性。在我的用户实体中,我有:Php 未序列化的ORM对象类型列,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我正在开发一个RESTAPI,我有一个object类型的列作为我的用户类的属性。在我的用户实体中,我有: /** * @var stdClass * @Serializer\Expose * @Serializer\Groups({"list", "details"}) * @ORM\Column(type="object", name="notifications") */ protected $notifications; 创建新用户时,我使用不同通知的默认值初始化此字段,如下所
/**
* @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);
我可以看到此字段正在正确写入数据库。在my db的notifications
列下,我得到:
O:8:"stdClass":3:{s:11:"voucherSold";b:1;s:15:"voucherRedeemed";b:1;s:13:"newConnection";b:1;}
但当我通过API加载此用户时,在notifications字段中会出现一个空对象:
{
"resource": "vendors/39",
"id": 39,
"email": "test@user.com",
"notifications": {},
"company": "",
"address": "",
"city": "",
"state": "",
"zipcode": "",
"phone": "",
"website": ""
}
我不明白为什么这个值没有通过。有什么想法吗?谢谢。json编码器可能只将数组转换为json,只需将对象设置为对象
{}
,而无需查看字段等
在返回前,请尝试将$\u通知
对象强制转换为数组:
$user->setNotifications((array) $_notifications);
我最好的猜测是json编码器不能处理仅对象的数组。在返回通知对象之前,您可能需要将其强制转换为数组。@samlev就是这样做的。如果你提出这样的答复,我将欣然接受。干杯