Reputation: 495
I've got a problem with JMSSerializerBundle.
I have my entity AGVote there :
<?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();
}
}
I have made a controller that juste return me an AGVote Entity in Json :
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;
}
}
I have a response in Json but it is a error saying :
[{"message":"The Response content must be a string or object implementing __toString(), \"object\" given.","class":"UnexpectedValueException","trace":
In fact I have already implement a __toString() method inside of all my entities.
Does anyone have an idea ?
Thanks you :)
Upvotes: 1
Views: 4706
Reputation: 7745
When you call the serialize
method on the $serializer
, it returns the serialized data (a string).
The problem is that you do not use this returned value, and create the response with the $serializer
itself, which makes no sense.
First, store the serialized $entity:
$serializedEntity = $serializer->serialize($entity, 'json');
Then, you can return a new response using with string:
return new Response($serializedEntity, 200, array('Content-Type' => 'application/json'));
Upvotes: 8