我遇到了 JMSSerializerBundle 的问题。
我在那里有我的实体 AGVote:
<?php
namespace K\AGBundle\Entity;
use JMS\SerializerBundle\Annotation\Type;
use JMS\SerializerBundle\Annotation\Accessor;
use JMS\SerializerBundle\Annotation\AccessType;
use JMS\SerializerBundle\Annotation\Exclude;
use JMS\SerializerBundle\Annotation\ExclusionPolicy;
use Doctrine\ORM\Mapping as ORM;
/**
* K\AGBundle\Entity\AGVote
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*
*/
/*
*
/** @AccessType("public_method") */
class AGVote
{
/**
* @Type("integer")
* @Accessor(getter="getId")
*/
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="text")
* @Accessor(getter="getQuestion")
* @Type("text")
*/
public $question;
/**
* @ORM\Column(type="smallint")
* @Type("integer")
* @Accessor(getter="getActif")
*/
public $actif;
/**
* @ORM\ManyToOne(targetEntity="\K\KBundle\Entity\Users", cascade={"all"})
* @Exclude
*/
protected $users;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set question
* Nb : Only AG admin can set a question
* @param text $question
*/
public function setQuestion($question)
{
$this->question = $question;
}
/**
* Get question
*
* @return text
*/
public function getquestion()
{
return $this->question;
}
/**
* Set actif
*
* @param smallint $actif
*/
public function setActif($actif)
{
$this->actif = $actif;
}
/**
* Get actif
*
* @return smallint
*/
public function getActif()
{
return $this->actif;
}
/**
* Set Users
*
* @param K\KBundle\Entity\Province $Users
*/
public function setUsers(\K\KBundle\Entity\Users $users)
{
$this->users = $users;
}
/**
* Get Users
*
* @return K\KBundle\Entity\Users
*/
public function getUsers()
{
return $this->users;
}
public function __toString()
{
return $this->getquestion();
}
}
我制作了一个控制器,它在 Json 中向我返回了一个 AGVote 实体:
public function jsonvoteAction($id) {
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('KAGBundle:AGVote')->findOneById($id);
if ($entity->getActif() == 1) {
$serializer = $this->container->get('serializer');
$serializer->serialize($entity, 'json');
$response = new Response($serializer);
return $reponse;
}
}
我在 Json 中有一个响应,但它是一个错误说:
[{"message":"响应内容必须是实现__toString()的字符串或对象,\"object\"给出。","class":"UnexpectedValueException","trace":
事实上,我已经在我的所有实体中实现了一个 __toString() 方法。
有人有想法吗?
谢谢 :)