JMSSerializerBundle RuntimeException: you must define a type for Entity::$field

tolgap picture tolgap · Nov 29, 2012 · Viewed 8.1k times · Source

I'm having this issue with JMSSerializerBundle. It basically gives me an exception for something that I've already done. This is my entity:

Edited to avoid confusion about annotation lines

<?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;

As you can see, I've added @Type() annotations for the fields, but it still gives me the exception when I call:

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

I've checked and the $content variable is not empty and has all the fields mapped in JSON format.

In my Monolog files, this is the exact Exception:

[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

Why does it still give me this exception?

Answer

Thomas Kelley picture Thomas Kelley · Nov 30, 2012

I'm fairly certain it's because you have two comment strings with different pieces of the whole annotation. Symfony only looks at the comment string directly preceding the class member.

Try replacing:

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

with:

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

(and in every other place you have these duplicate annotation comments)

It's only a guess, but I think it'll fix it. When I tried doing this:

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

...Symfony gave me this error:

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