gpt4 book ai didi

symfony - JMSSerializerBundle RuntimeException:您必须为 Entity::$field 定义类型

转载 作者:行者123 更新时间:2023-12-02 16:09:38 28 4
gpt4 key购买 nike

我在使用 JMSSerializerBundle 时遇到了此问题。它基本上给了我一个我已经做过的事情的异常(exception)。这是我的实体:

进行编辑以避免注释行混淆

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

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

最佳答案

我相当确定这是因为您有两个注释字符串,其中包含整个注释的不同部分。 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.

关于symfony - JMSSerializerBundle RuntimeException:您必须为 Entity::$field 定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13636180/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com