Este foro ya no está activo, así que no puedes publicar nuevas preguntas ni responder a las preguntas existentes.

Error intentando serializar una colección para respuesta JSON

23 de septiembre de 2015

Buenas noches:

Estoy intentando serializar una colección en JSON para devolver una respuesta AJAX.

El código que tengo para serializar es el siguiente:

[...]
        $visitas = $response->getVisitas(); // respuesta de una consulta DQL
 
        $encoder = new JsonEncoder();
        $normalizer = new ObjectNormalizer();
 
        $normalizer->setCircularReferenceHandler(function ($object) {
            return $object->getId();
        });
 
        $serializer = new Serializer(array($normalizer), array($encoder));
 
        //$serializer = $this->get('serializer');
        $json = $serializer->serialize($visitas, 'json');
 
        return new JsonResponse(array(
            'visitas' => $json,
        ), 200);

El error que me está devolviendo es el siguiente: Runtime Notice: Accessing static property Proxies__CG__\AppBundle\Entity\Paciente::$lazyPropertiesDefaults as non static

Y las entidades involucradas:

/**
 * Visita
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Visita
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
 
    /**
     * @ORM\ManyToOne(targetEntity="Paciente", inversedBy="visitas", fetch="EAGER")
     */
    private $paciente;
[...]
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
 
    /**
     * Set paciente
     *
     * @param string $paciente
     * @return Visita
     */
    public function setPaciente($paciente)
    {
        $this->paciente = $paciente;
 
        return $this;
    }
 
    /**
     * Get paciente
     *
     * @return string 
     */
    public function getPaciente()
    {
        return $this->paciente;
    }
[...]
 
    public function __construct()
    {
        $this->eventos = new ArrayCollection();
        $this->estado = "pendiente";
    }
[...]
}
/**
 * Paciente
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Paciente extends DatosContacto
{
    /**
     * @var string
     *
     * @ORM\Column(name="Nombre", type="string", length=50)
     */
    private $nombre;
[...]
    /**
     * @ORM\OneToMany(targetEntity="Visita", mappedBy="paciente")
     */
    private $visitas;
 
    /**
     * Add visitas
     *
     * @param \AppBundle\Entity\Visita $visitas
     * @return Paciente
     */
    public function addVisita(\AppBundle\Entity\Visita $visitas)
    {
        $this->visitas[] = $visitas;
 
        return $this;
    }
 
    /**
     * Remove visitas
     *
     * @param \AppBundle\Entity\Visita $visitas
     */
    public function removeVisita(\AppBundle\Entity\Visita $visitas)
    {
        $this->visitas->removeElement($visitas);
    }
 
    /**
     * Get visitas
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getVisitas()
    {
        return $this->visitas;
    }
[...]
    public function __construct()
    {
        if (!$this->visitas) $this->visitas = new ArrayCollection();
        if (!$this->datosClinicos) 
        {
            $this->datosClinicos = new \AppBundle\Entity\DatosClinicos();
        }
        if (!$this->imagenes) $this->imagenes = new ArrayCollection();
    }
}

Espero que me puedan ayudar.

Muchas gracias y un saludo.


Respuestas

#1

Buenas tardes:

Al final he conseguido resolverlo, dejo la respuesta por si le sirviese a alguien.

Saludos.

public function obtenerVistasAction()
    {
        $request = new DV\ObtenerVisitasRequest();
        $request->setHoy(true);
        $request->setCompletas(false);
        $response = $this->get('utils.visita')->ObtenerVisitas($request);
        $visitas = $response->getVisitas();
 
        $encoder = new JsonEncoder();
        $normalizer = new GetSetMethodNormalizer();
        $normalizer->setIgnoredAttributes(array('visitas','datosClinicos','titular'));
 
        $serializer = new Serializer(array($normalizer), array($encoder));
        $json = $serializer->serialize($visitas, 'json');
 
        return new JsonResponse(array(
            'visitas' => $json,
        ), 200);
    }

@jesusjbm

29 septiembre 2015, 20:24
#2

Gracias por publicar la respuesta completa.

@javiereguiluz

29 septiembre 2015, 20:26