【问题标题】:JMSSerializerBundle RuntimeException: you must define a type for Entity::$fieldJMSSerializerBundle RuntimeException:您必须为 Entity::$field 定义一个类型
【发布时间】:2012-11-29 22:47:16
【问题描述】:

我在使用 JMSSerializerBundle 时遇到了这个问题。对于我已经完成的事情,它基本上给了我一个例外。这是我的实体:

为避免混淆注释行而进行了编辑

<?php

namespace My\ProjectBundle\Entity;
use JMS\SerializerBundle\Annotation\Type;

use Doctrine\ORM\Mapping as ORM;

/**
 * My\ProjectBundle\Entity\Music
 * 
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="My\ProjectBundle\Entity\MusicRepository")
 */
class Music extends Post
{
/**
 * @var integer $id
 * 
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @var string $album
 *
 * @ORM\Column(name="album", type="string")
 * @Type("string")
 */
protected $album;

/**
 * @var string $artist
 *
 * @ORM\Column(name="artist", type="string")
 * @Type("string")
 */
protected $artist;

/**
 * @var integer $duration
 *
 * @ORM\Column(name="duration", type="bigint")
 * @Type("int")
 */
protected $duration;

/**
 * @var string $title
 *
 * @ORM\Column(name="title", type="string")
 * @Type("string")
 */
protected $title;

/**
 * @var array $genres
 *
 * @ORM\Column(name="genres", type="array")
 * @Type("array")
 */
protected $genres;

如您所见,我为字段添加了@Type() 注释,但是当我调用时它仍然给我异常:

$listenedMusic = $serializer->deserialize($content, 'My\ProjectBundle\Entity\Music', 'json');

我检查过,$content 变量不为空,并且所有字段都映射为 JSON 格式。

在我的 Monolog 文件中,这是确切的例外:

[2012-11-29 23:39:07] request.CRITICAL: JMS\SerializerBundle\Exception\RuntimeException: 
You must define a type for My\ProjectBundle\Entity\Music::$album. (uncaught exception) 
at /vendor/jms/serializer-bundle/JMS/SerializerBundle/Serializer/GenericDeserializationVisitor.php line 177

为什么它仍然给我这个异常?

【问题讨论】:

  • 您不需要将@Type 与序列化程序已经从实体注释中获取的简单变量一起使用。只有保存相关对象的字段才需要该类型。Type("array")
  • 好吧,为什么我得到Exception,而当它只是一个字符串时,我必须为$album 设置一个Type

标签: symfony doctrine doctrine-orm symfony-2.1 jmsserializerbundle


【解决方案1】:

我很确定这是因为您有两个注释字符串,其中包含整个注释的不同部分。 Symfony 只查看类成员前面的注释字符串。

尝试替换:

/** @Type("string")*/
/**
 * @var string $album
 *
 * @ORM\Column(name="album", type="string")*/
protected $album;

与:

/** 
 * @Type("string")
 *
 * @var string $album
 *
 * @ORM\Column(name="album", type="string")*/
protected $album;

(并且在其他所有地方都有这些重复的注释 cmets)

这只是一个猜测,但我认为它会解决它。当我尝试这样做时:

class Something
{
    /**
     * @var integer $id
     * 
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    /**
     * 
     */
    private $id;
}

...Symfony 给了我这个错误:

No identifier/primary key specified for Entity 'SomeApp\SomeBundle\Entity\Something'. Every Entity must have an identifier/primary key.

【讨论】:

  • 没关系。当它不起作用时,我只是尝试了不同的东西。我在与 Doctrine 相同的注释行中使用它。
【解决方案2】:

我已通过将整个项目更新为 dev-master 包来解决此问题。似乎是 JMSSerializer 中的一个错误,因为没有修改任何代码,我就停止了这个错误。

【讨论】:

    【解决方案3】:
    /**
     * @var integer $duration
     *
     * @ORM\Column(name="duration", type="bigint")
     * @Type("int")
     */
    protected $duration;
    

    类型“int”不存在用于序列化,您必须使用“整数”。

    【讨论】:

      猜你喜欢
      • 2015-08-16
      • 2022-11-04
      • 1970-01-01
      • 2018-09-17
      • 2019-05-24
      • 2018-03-24
      • 2018-02-13
      • 2013-03-04
      相关资源
      最近更新 更多