Controller:
public function newUserAction(Request $request)
{
$errores = array();
$usuario = new Usuario();
$foto = $request->files->get('foto');
$email = $request->request->get('email');
$username = $request->request->get('username');
$pass = $request->request->get('password');
if ($email == null){
array_push($errores, 'El Email no puede estar vacío') ;
}
if ($pass == null){
array_push($errores, 'El Password no puede estar vacío') ;
}
$helper = $this->get('my_service.helper');
if (count($errores) == 0){
$fecha_ahora = new \DateTime("now");
$usuario->setEmail($email);
$usuario->setUsername($username);
$em = $this->getDoctrine()->getManager();
$perfil = $em->getRepository('MainBundle:Perfiles')->find(1);
$usuario->setPerfil($perfil);
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($usuario);
$salt = $helper->randomString(25,true,true,true);
$usuario->setSalt($salt);
$password = $encoder->encodePassword($pass, $salt);
$usuario->setPassword($password);
$confirmationToken = $helper->randomString(30,true,true,true);
$usuario->setConfirmationToken($confirmationToken);
$usuario->setFechaRegistro($fecha_ahora);
$usuario->setFechaUltimoLogin($fecha_ahora);
if ($foto!= null){
$helper->guardaFotoUsuario($foto);
}
$em = $this->getDoctrine()->getManager();
$em->persist($usuario);
$em->flush();
}
$usuarioPresenter = $helper->getUsuarioPresenter($usuario);
if (count($errores) > 0){
foreach ($errores as $error){
$usuarioPresenter->addError($error);
}
}
return $usuarioPresenter;
}
User entities and profiles:
<?php
namespace ...\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Type;
/**
* @ORM\Entity
* @ORM\Table(name="usuario")
*
*/
class Usuario {
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
**/
private $id;
/**
* @var string
*
* @ORM\Column(type="string",nullable=false)
*/
private $email;
/**
* @var string
*
* @ORM\Column(type="string",nullable=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(type="string",nullable=false)
*/
private $password;
/**
* @var string
*
* @ORM\Column(type="string",nullable=true)
*/
private $salt;
/**
* @var string
*
* @ORM\Column(type="string",nullable=true)
*/
private $confirmationToken;
/**
* @var boolean
*
* @ORM\Column(type="boolean",nullable=false)
*/
private $activo = false;
/**
* @var string
*
* @ORM\Column(type="string",nullable=true)
*/
private $sessionId;
/**
* @var string
*
* @ORM\Column(type="string",nullable=true, length=20)
*/
private $cifDni;
/**
* @var string
*
* @ORM\Column(type="string",nullable=true, length=14)
*/
private $tlf;
/**
* @Type("array")
* @ORM\Column(name="fechaRegistro", type="datetime",nullable=true)
**/
private $fechaRegistro;
/**
* @Type("array")
* @ORM\Column(name="fechaUltimoLogin", type="datetime",nullable=true)
**/
private $fechaUltimoLogin;
//relacion entre el usuario y su perfil
/**
* @Type("array")
* @ORM\ManyToOne(targetEntity="Perfiles",inversedBy="usuarios")
* @ORM\JoinColumn(name="perfil", referencedColumnName="id")
*/
private $perfil;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set email
*
* @param string $email
*
* @return Usuario
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set username
*
* @param string $username
*
* @return Usuario
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* @param string $password
*
* @return Usuario
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set salt
*
* @param string $salt
*
* @return Usuario
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set confirmationToken
*
* @param string $confirmationToken
*
* @return Usuario
*/
public function setConfirmationToken($confirmationToken)
{
$this->confirmationToken = $confirmationToken;
return $this;
}
/**
* Get confirmationToken
*
* @return string
*/
public function getConfirmationToken()
{
return $this->confirmationToken;
}
/**
* Set activo
*
* @param boolean $activo
*
* @return Usuario
*/
public function setActivo($activo)
{
$this->activo = $activo;
return $this;
}
/**
* Get activo
*
* @return boolean
*/
public function getActivo()
{
return $this->activo;
}
/**
* Set sessionId
*
* @param string $sessionId
*
* @return Usuario
*/
public function setSessionId($sessionId)
{
$this->sessionId = $sessionId;
return $this;
}
/**
* Get sessionId
*
* @return string
*/
public function getSessionId()
{
return $this->sessionId;
}
/**
* Set cifDni
*
* @param string $cifDni
*
* @return Usuario
*/
public function setCifDni($cifDni)
{
$this->cifDni = $cifDni;
return $this;
}
/**
* Get cifDni
*
* @return string
*/
public function getCifDni()
{
return $this->cifDni;
}
/**
* Set tlf
*
* @param string $tlf
*
* @return Usuario
*/
public function setTlf($tlf)
{
$this->tlf = $tlf;
return $this;
}
/**
* Get tlf
*
* @return string
*/
public function getTlf()
{
return $this->tlf;
}
/**
* Set fechaRegistro
*
* @param \DateTime $fechaRegistro
*
* @return Usuario
*/
public function setFechaRegistro($fechaRegistro)
{
$this->fechaRegistro = $fechaRegistro;
return $this;
}
/**
* Get fechaRegistro
*
* @return \DateTime
*/
public function getFechaRegistro()
{
return $this->fechaRegistro;
}
/**
* Set fechaUltimoLogin
*
* @param \DateTime $fechaUltimoLogin
*
* @return Usuario
*/
public function setFechaUltimoLogin($fechaUltimoLogin)
{
$this->fechaUltimoLogin = $fechaUltimoLogin;
return $this;
}
/**
* Get fechaUltimoLogin
*
* @return \DateTime
*/
public function getFechaUltimoLogin()
{
return $this->fechaUltimoLogin;
}
/**
* Set perfil
*
* @param \TaurusUrsusApiRest\MainBundle\Entity\Perfiles $perfil
*
* @return Usuario
*/
public function setPerfil(\TaurusUrsusApiRest\MainBundle\Entity\Perfiles $perfil = null)
{
$this->perfil = $perfil;
return $this;
}
/**
* Get perfil
*
* @return \TaurusUrsusApiRest\MainBundle\Entity\Perfiles
*/
public function getPerfil()
{
return $this->perfil;
}
}
<?php
namespace ...\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Perfil
*
* @ORM\Table()
* @ORM\Entity
*/
class Perfiles
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
**/
protected $id;
/**
* @var string
*
* @ORM\Column(name="nombrePerfil", type="string", length=250)
**/
protected $nombrePerfil;
/**
* @ORM\OneToMany(targetEntity="Usuario", mappedBy="perfil")
*/
protected $usuarios;
/**
* Constructor
*/
public function __construct()
{
$this->usuarios = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nombrePerfil
*
* @param string $nombrePerfil
*
* @return Perfiles
*/
public function setNombrePerfil($nombrePerfil)
{
$this->nombrePerfil = $nombrePerfil;
return $this;
}
/**
* Get nombrePerfil
*
* @return string
*/
public function getNombrePerfil()
{
return $this->nombrePerfil;
}
/**
* Add usuario
*
* @param \TaurusUrsusApiRest\MainBundle\Entity\Usuario $usuario
*
* @return Perfiles
*/
public function addUsuario(\TaurusUrsusApiRest\MainBundle\Entity\Usuario $usuario)
{
$this->usuarios[] = $usuario;
return $this;
}
/**
* Remove usuario
*
* @param \TaurusUrsusApiRest\MainBundle\Entity\Usuario $usuario
*/
public function removeUsuario(\TaurusUrsusApiRest\MainBundle\Entity\Usuario $usuario)
{
$this->usuarios->removeElement($usuario);
}
/**
* Get usuarios
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsuarios()
{
return $this->usuarios;
}
}
JSON Resume:
{
"entity": {
"email": "[email protected]",
"username": "Usuario desde postman",
"password": "$2y$13$ZEE1QUB2JGEjcXMzOVcpe.0jx06mmqOQxUzj8f1OxtYhTxmkmHEU.",
"salt": "dA5A@v$a#qs39W)xzrBLF($oA",
"confirmation_token": "*Ud0OWeP5_M7;PUXD&(Rpwtcsz3|J&",
"activo": false,
"fecha_registro": {
"date": "2018-01-12 09:57:11.000000",
"timezone_type": 3,
"timezone": "Europe/Berlin"
},
"fecha_ultimo_login": {
"date": "2018-01-12 09:57:11.000000",
"timezone_type": 3,
"timezone": "Europe/Berlin"
},
"perfil": []
},
"ruta_foto": "/imgusers/default/",
"errores": []
}
Why is the Profile class empty? can anybody help me? another thing that happens to me here is that the dates I must return them as an array because if I do this @Type ("DateTime < 'Y-m-d' >") they come empty. Any suggestions or help? thank you very much.