I have this code, from an entity:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\Accessor;
use AppBundle\Validator\Constraints as ImplikaAssert;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use AppBundle\Entity\Provincia as Provincia;
use AppBundle\Repository\ProvinciaRepository;
/**
* Alumno
*
* @ORM\Table(name="alumno")
* @ORM\Entity(repositoryClass="AppBundle\Repository\AlumnoRepository")
*
*/
class Alumno
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Type("integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=100)
* @Type("string")
*/
private $nombre;
/**
* @var string
*
* @ORM\Column(name="apellido_1", type="string", length=100)
* @Type("string")
*/
private $apellido1;
/**
* @var string
*
* @ORM\Column(name="apellido_2", type="string", length=100, nullable=true)
* @Type("string")
*/
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)
* @Type("string")
*/
private $email;
/**
* @ORM\ManyToOne(targetEntity="Provincia")
* @ORM\JoinColumn(name="provincia_id", referencedColumnName="id", onDelete="SET NULL")
* @Type("string")
* @Accessor(getter="getProvinciaNombre",setter="setProvinciaNombre")
*/
private $provincia;
public function __toString()
{
return $this->apellido1 . ' ' .
$this->apellido2 . ', ' .
$this->nombre . ' - ' .
$this->dni;
}
/**
* Constructor
*/
public function __construct()
{
$this->matriculas = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set nombre
*
* @param string $nombre
*
* @return Alumno
*/
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
/**
* Get nombre
*
* @return string
*/
public function getNombre()
{
return $this->nombre;
}
/**
* Set apellido1
*
* @param string $apellido1
*
* @return Alumno
*/
public function setApellido1($apellido1)
{
$this->apellido1 = $apellido1;
return $this;
}
/**
* Get apellido1
*
* @return string
*/
public function getApellido1()
{
return $this->apellido1;
}
/**
* Set apellido2
*
* @param string $apellido2
*
* @return Alumno
*/
public function setApellido2($apellido2)
{
$this->apellido2 = $apellido2;
return $this;
}
/**
* Get apellido2
*
* @return string
*/
public function getApellido2()
{
return $this->apellido2;
}
/**
* Set email
*
* @param string $email
*
* @return Alumno
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set provincia
*
* @param string $provincia
*
* @return Alumno
*/
public function setProvincia($provincia)
{
$this->provincia = $provincia;
return $this;
}
/**
* Get provincia
*
* @return string
*/
public function getProvincia()
{
return $this->provincia;
}
public function getProvinciaNombre()
{
$provincia = new Provincia();
$provincia = $this->provincia;
return $provincia->getNombre();
}
public function setProvinciaNombre($nombre)
{
$this->provincia = ProvinciaRepository->getByNombre($nombre);
}
}
As you can see I have a property that deserializar uses the annotation @Accessor. What I would like is for the object when it deserializes in the province to insert a province object that I already have in the database.
My first idea was to create a custom repository and try to call it.
My second idea has been to generate a service to call the repository, but it has not solved either.
Any other ideas ??