JMSSerializerBundle RuntimeException:您必须为Entity :: $字段定义类型

时间:2012-11-29 22:47:17

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

我遇到了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

为什么它仍然给我这个例外?

3 个答案:

答案 0 :(得分:3)

我很确定这是因为你有两个注释字符串,包含整个注释的不同部分。 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;

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

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

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.

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

输入&#39; int&#39;对于序列化不存在,您必须使用&#39;整数&#39;。

相关问题