I have the following entity:
class Alumno
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=100)
*/
private $nombre;
/**
* @var string
*
* @ORM\Column(name="apellido_1", type="string", length=100)
*/
private $apellido1;
/**
* @var string
*
* @ORM\Column(name="apellido_2", type="string", length=100, nullable=true)
*/
private $apellido2;
/**
* @var string
* @ImplikaAssert\IsValidIdNumber
*
* @ORM\Column(name="dni", type="documento_identidad", length=20)
* @Type("string")
*/
private $dni;
/**
* @var \DateTime
*
* @ORM\Column(name="fecha_nacimiento", type="date")
* @Type("DateTime<'Y-m-d'>")
*/
private $fecha_nacimiento;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=100)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="telefono_1", type="telefono", length=18)
* @Type("string")
*/
private $telefono1;
/**
* @var string
*
* @ORM\Column(name="telefono_2", type="telefono", length=18, nullable=true)
* @Type("string")
*/
private $telefono2;
/**
* @var string
*
* @ORM\Column(name="direccion", type="string", length=255, nullable=true)
*/
private $direccion;
/**
* @var string
*
* @ORM\Column(name="poblacion", type="string", length=100, nullable=true)
*/
private $poblacion;
/**
* @ORM\ManyToOne(targetEntity="Provincia")
* @ORM\JoinColumn(name="provincia_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $provincia;
.....
with its corresponding get and set. Being symfony 3 I use the use JMS \ Serializer \ Serializer; to serialize and deserialize
the issue is that I want to use an array of the style
$json = array(
"nombre"=>"Alumno de prueba",
"apellido1"=>"Apellido1prueba",
"apellido2"=>"Apellido2prueba",
"dni"=>"11110000R",
"fecha_nacimiento"=>"1978-12-12",
"email"=>"[email protected]",
"telefono1"=>"666666666",
"telefono2"=>"766666666",
"direccion"=>"c/prueba 123",
"poblacion"=>"",
"provincia"=> "Valencia",
"codigopostal"=> "20600",
);
To create the object, but the province does not know exactly how to make it automatically deserialize. I have to make a custom deserializer, or something? more than other things for not doing this.
$arrayjson = json_decode($json,true);
$em = $this->getDoctrine()->getManager();
if (isset($arrayjson["nombre"]) and !empty($arrayjson["nombre"])) {
$entidad->setNombre($arrayjson["nombre"]);
}
if (isset($arrayjson["apellido1"]) and !empty($arrayjson["apellido1"])) {
$entidad->setApellido1($arrayjson["apellido1"]);
}
if (isset($arrayjson["apellido2"])) {
$entidad->setApellido2($arrayjson["apellido2"]);
}
if (isset($arrayjson["email"]) and !empty($arrayjson["email"])) {
$entidad->setEmail($arrayjson["email"]);
}
if (isset($arrayjson["telefono1"]) and !empty($arrayjson["telefono1"])) {
$entidad->setTelefono1($arrayjson["telefono1"]);
}
if (isset($arrayjson["telefono2"]) and !empty($arrayjson["telefono2"])) {
$entidad->setTelefono2($arrayjson["telefono2"]);
}
if (isset($arrayjson["direccion"]) and !empty($arrayjson["direccion"])) {
$entidad->setDireccion($arrayjson["direccion"]);
}
if (isset($arrayjson["poblacion"]) and !empty($arrayjson["poblacion"])) {
$entidad->setPoblacion($arrayjson["poblacion"]);
}
if (isset($arrayjson["provincia"]) and !empty($arrayjson["provincia"])) {
$entidad->setProvincia(
$em->getRepository('AppBundle:Provincia')->getByNombre($arrayjson["provincia"])
);
}
if (isset($arrayjson["observaciones"]) and !empty($arrayjson["observaciones"])) {
$entidad->setObservaciones($arrayjson["observaciones"]);
}
if (isset($arrayjson["dni"]) and !empty($arrayjson["dni"])) {
$entidad->setDni($arrayjson["dni"]);
}
if (isset($arrayjson["codigoPostal"]) and !empty($arrayjson["codigoPostal"])) {
$entidad->setCodigoPostal($arrayjson["codigoPostal"]);
}
if (isset($arrayjson["fecha_nacimiento"]) and !empty($arrayjson["fecha_nacimiento"])) {
$entidad->setFechaNacimiento(date_create($arrayjson["fecha_nacimiento"]));
}
}
I think it's a hooligan when there is a deserializer. Thanks in advance