Controller sends null value in a set command in Symfony2

76 views Asked by At

I have a controller that inserts new records, but sends NULL value to idUsuario which is the ID of the user who is creating the new record.

I have already checked the entity file, the estructure of the table, even setting a manual value like 1. I have tried the sql command directly in mysql command window and the problem in that idUsuario is comming NULL.

Controller:

  function createAction ....
    $user = $this->getUser();
    $idUser = $user->getId();

    $entity = new InProveedor();
    $entity->setIdUsuario($idUser);
    $em = $this->getDoctrine()->getManager();
    $em->persist($entity);
    $em->flush();

Entity:

/**
 * @ORM\ManyToOne(targetEntity="Nival\AccesoBundle\Entity\AdUsuario", 
   inversedBy="proveedorUsuario")
 * @ORM\JoinColumn(name="id_usuario", referencedColumnName="id_usuario")
 */
protected $proveedorUsuario;

/**
 * @var integer
 *
 * @ORM\Column(name="id_usuario", type="integer", nullable=false)
 */
private $idUsuario;

/**
 * Set idUsuario
 *
 * @param integer $idUsuario
 * @return InProveedor
 */
public function setIdUsuario($idUsuario)
{
    $this->idUsuario = $idUsuario;

    return $this;
}

/**
 * Get idUsuario
 *
 * @return integer
 */
public function getIdUsuario()
{
    return $this->idUsuario;
}

An exception occurred while executing 'INSERT INTO in_proveedor (razon_social, nombre_comercial, direccion, telefono, email, nrc, nit, contacto, fecha_ingreso, id_forma_pago, dias_entrega, id_aut1_asig, id_notificado, fecha_notificado, id_aut1, fecha_aut1, id_categoria, id_usuario, fecha_creado, activo, id_empresaa) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["GM Consulting SA de CV", "GM Consulting SA de CV", "9 Calle poniente 81 Ave norte col Escal\u00f3n 7-29, San Salvador", "22875000", "[email protected]", "22222", "222", "Astrid Cuadra", "2019-05-28", 1, null, 1, null, null, null, null, 1, null, "2019-05-28 20:50:41", 0, "2"]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id_usuario' cannot be null

So, it seems that even if I set a value for field idUsuario, it always return as NULL. Please help me out.

1

There are 1 answers

0
Pedro Elias Aguilar On BEST ANSWER

Well, what I did it was so weird, I had to remove the relationship beteewen tables, and save a record with the ID, then I put back the relationship in the entity files (both: AdUsuario and InProveedor) and it worked properly.