Using SF 2.8 and serializer component.
I have 2 entities with relationships many to one.
ENTITY USUARIO
...
class Usuario implements UserInterface
{
...
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=50)
*/
private $nombre;
/**
* @var string
*
* @ORM\Column(name="apellidos", type="string", length=100)
*/
private $apellidos;
...getters and setters...
ENTITY PLAN
...
class Plan
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="\UsuariosBundle\Entity\Usuario", cascade={"persist"})
* @ORM\JoinColumn(name="usuario_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $usuario;
/**
* @var array
*
* @ORM\Column(name="json_dieta", type="json_array")
*/
private $jsonDieta;
...getters and setters...
As you can see, property $usuario (entity plan) is joined with $id (entity usuario).
My intention is to save a json in a field of the table plan of the DB to recover the data when I need them; for do that, first I have to serialize the data and save the to the DB (DONE!) but when I have to deserialize them for recovering the data and transform them to objects I get this error:
Type error: Argument 1 passed to ComponentesBundle\Entity\Receta::setUsuario() must be an instance of UsuariosBundle\Entity\Usuario, array given, called in C:\xampp7\htdocs\Dietas\vendor\symfony\serializer\Normalizer\GetSetMethodNormalizer.php on line 118
MY CONTROLLER
...
$em = $this->getDoctrine()->getManager();
$plan =$em->getRepository('UsuariosBundle:Plan')->findOneByUsuario($user->getId());
$serializer = new Serializer(
array(new GetSetMethodNormalizer(), new ArrayDenormalizer()),
array(new JsonEncoder())
);
$jsonDeserializado = $serializer->deserialize($plan->getJsonDieta(), 'ComponentesBundle\Entity\Receta[][][]', 'json');
dump($jsonDeserializado);
...
Any idea how can I solve this?