【发布时间】:2015-08-11 17:21:24
【问题描述】:
为什么除了 json 中的数据之外的所有值都用 null 实例化新实体,为什么实体构造函数没有设置默认值 - 在构造函数中放置 die() 永远不会被执行。
更新:
好吧,深入研究代码,当没有找到托管实体时,JMSS 将使用学说实例化器类来创建实体——它的唯一工作是在不调用构造函数的情况下创建实体。是否有一个原因?这是在JMS\Serializer\Construction\UnserializeObjectConstructor
我已将对象构造函数配置为使用 JMS 编写的学说对象构造函数,但无论有没有这个,都会出现同样的问题。
jms_serializer.object_constructor:
alias: jms_serializer.doctrine_object_constructor
public: false
现有实体可以顺利更新,但新实体缺少所有构造函数集默认值。
在“字段”下,元素 0 存在,元素 1 是新的。
array (size=3)
'id' => int 2
'name' => string 'Categories' (length=10)
'fields' =>
array (size=2)
0 =>
array (size=7)
'id' => int 49
'displayName' => string 'Car Branded' (length=11)
'type' => string 'checkboxlist' (length=12)
'required' => boolean false
'disabled' => boolean false
'name' => string 'h49' (length=3)
1 =>
array (size=3)
'type' => string 'email' (length=5)
'name' => string 'field3491' (length=9)
'displayName' => string 'Email' (length=5)
反序列化后的实体是这样的:
object(stdClass)[2000]
public '__CLASS__' => string 'AppBundle\Entity\FormElement' (length=28)
public 'id' => null
public 'label' => string 'Email' (length=5)
public 'type' => string 'email' (length=5)
public 'defaultValue' => null
public 'required' => null
public 'mappedField' => null
public 'garbageCollection' => null
public 'sortOrder' => null
public 'disabled' => null
public 'uuid' => null
public 'form' => null
public 'multiOptions' => null
public 'filters' => null
public 'submissions' => null
实体构造函数:
public function __construct()
{
$this->required = false;
$this->disabled = false;
$this->garbageCollection = false;
$this->sortOrder = 0;
$this->type = 'text';
}
最后这就是我反序列化的方式:
$serializer = $this->get('jms_serializer');
$entryForm = $serializer->deserialize($json_data, 'AppBundle\Entity\EntryForm', 'json');
【问题讨论】:
标签: php symfony doctrine-orm jmsserializerbundle