2013-04-14 16 views
3

基本上我想反序列化json到用户对象。像传递到解串器这个FOSUserBundle反序列化为用户对象抛出“你必须定义一个类型”

$serializer = $this->get('jms_serializer'); 

$data = $serializer->deserialize($this->get('request')->getContent(), 
           'My\FooBundle\Entity\User', 'json'); 

请求数据:

{"id":2,"username":"[email protected]","username_canonical":"[email protected]", ... } 

时抛出错误:

request.CRITICAL: Uncaught PHP Exception JMS\Serializer\Exception\RuntimeException: "You must define a type for FOS\UserBundle\Model\User::$username." at ...\vendor\jms\serializer\src\JMS\Serializer\GenericDeserializationVisitor.php line 170 [] []

我使用SF 2.3。是否有什么必须为fos用户类的反序列化设置?任何想法为什么我得到这个消息,以及如何解决它?

+0

你应该看看[这个问题](http://stackoverflow.com/questions/12960141/jmsserializerbundle-no-control-over-third-party-meta-data)。我和FOSUserBundle有同样的问题,因为这些字段没有在你的My \ Foo \ Entity \ User模型中定义,而是在'FOS \ UserBundle \ Model \ User'模型中定义。 – tolgap

回答

7

你好,我刚刚有同样的问题今天终于找到了如何应对这种非问题,

在要反序列化对象的每个属性,捆想知道哪种类型你指望有后转换。 所以,你必须使用由包提供的注释\键入你的实体类和annote每个属性: http://jmsyst.com/libs/serializer/master/reference/annotations#type

要小心:粘贴是邪恶的!在参考示例中使用的语句是use JMS\Serializer\Annotation\Type;但应该是串行use JMS\SerializerBundle\Annotation\Type;

从我的代码为例真实路径:

use JMS\SerializerBundle\Annotation\Type; 
    /** 
    * @ORM\Entity 
    * @ORM\Table(name="ecro_form") 
    */ 
    class Form { 

    /** 
    * @ORM\ManyToOne(targetEntity="eCRO\Bundle\UserBundle\Entity\User", inversedBy="forms") 
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
    * @Type("eCRO\Bundle\UserBundle\Entity\User") 
    */ 
    protected $user; // inherits from FOSUser 

在FOSUser抽象类中使用注释(所在地:供应商\ FOS \ UserBundle \ Model \ User.php),以便序列化器知道如何完成它的工作。

希望能对您有所帮助!

相关问题