4
I think I’m doing something wrong in this class, because it is the only one of my system that is not persisting in the database.
What am I forgetting to do to make it work? The method incluir()
? If I run a var_dump($log)
, before calling persist($log)
, the result is a variable with all necessary data.
And worst of all it doesn’t make any mistakes either.
Class Log.php
namespace JN\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="logs")
* @ORM\HasLifecycleCallbacks
*/
class Log {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $data;
/**
* @ORM\Column(type="string", length=100)
*/
private $tabela;
/**
* @ORM\Column(type="integer")
*/
private $id_usuario;
/**
* @ORM\Column(type="string", length=200)
*/
private $usuario;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $id_empresa;
/**
* @ORM\Column(type="string", length=200, nullable=true)
*/
private $empresa;
/**
* @ORM\Column(type="string", length=20)
*/
private $ip;
/**
* @ORM\Column(type="string", length=100)
*/
private $evento;
/**
* @ORM\Column(type="text")
*/
private $sql;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $valores_antigos;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $valores_novos;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="datetime")
*/
private $updated_at;
/*--------------------------Filters --------------------------------------- */
/**
*
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->updated_at =new \DateTime('now');
if ($this->created_at == null) {
$this->created_at=new \DateTime('now');
}
}
/*--------------------------Constructor --------------------------------------- */
/*--------------------------Relations --------------------------------------- */
/*--------------------------Gets and Sets --------------------------------------- */
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
/**
* @return mixed
*/
public function getTabela()
{
return $this->tabela;
}
/**
* @param mixed $tabela
*/
public function setTabela($tabela)
{
$this->tabela = $tabela;
}
/**
* @return mixed
*/
public function getIdUsuario()
{
return $this->id_usuario;
}
/**
* @param mixed $id_usuario
*/
public function setIdUsuario($id_usuario)
{
$this->id_usuario = $id_usuario;
}
/**
* @return mixed
*/
public function getUsuario()
{
return $this->usuario;
}
/**
* @param mixed $usuario
*/
public function setUsuario($usuario)
{
$this->usuario = $usuario;
}
/**
* @return mixed
*/
public function getIdEmpresa()
{
return $this->id_empresa;
}
/**
* @param mixed $id_empresa
*/
public function setIdEmpresa($id_empresa)
{
$this->id_empresa = $id_empresa;
}
/**
* @return mixed
*/
public function getEmpresa()
{
return $this->empresa;
}
/**
* @param mixed $empresa
*/
public function setEmpresa($empresa)
{
$this->empresa = $empresa;
}
/**
* @return mixed
*/
public function getIp()
{
return $this->ip;
}
/**
* @param mixed $ip
*/
public function setIp($ip)
{
$this->ip = $ip;
}
/**
* @return mixed
*/
public function getEvento()
{
return $this->evento;
}
/**
* @param mixed $evento
*/
public function setEvento($evento)
{
$this->evento = $evento;
}
/**
* @return mixed
*/
public function getSql()
{
return $this->sql;
}
/**
* @param mixed $sql
*/
public function setSql($sql)
{
$this->sql = $sql;
}
/**
* @return mixed
*/
public function getValoresAntigos()
{
return $this->valores_antigos;
}
/**
* @param mixed $valores_antigos
*/
public function setValoresAntigos($valores_antigos)
{
$this->valores_antigos = $valores_antigos;
}
/**
* @return mixed
*/
public function getValoresNovos()
{
return $this->valores_novos;
}
/**
* @param mixed $valores_novos
*/
public function setValoresNovos($valores_novos)
{
$this->valores_novos = $valores_novos;
}
/**
* @return mixed
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* @return mixed
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
}
Logservice.php class
namespace JN\Entity;
use Doctrine\ORM\EntityManager;
class LogService {
private $em;
private $app;
public function __construct(EntityManager $em, $app)
{
$this->em=$em;
$this->app=$app;
}
public function incluir($sesion=null,$valoresNovos=null)
{
try{
$log = new Log();
$log->setEmpresa('');
$log->setIdEmpresa(0);
$log->setIdUsuario(0);
$log->setUsuario('');
$log->setData(new \DateTime('now'));
$log->setIp($_SERVER["REMOTE_ADDR"]);
$log->setEvento('Inclusão');
$log->setTabela($sesion['tabela']);
$log->setSql($sesion['sql']);
$log->setValoresAntigos('');
$log->setValoresNovos($valoresNovos);
$this->em->persist($log);
$this->em->flush();
return $log;
}catch (\Exception $ex){
$this->app->abort(406, 'Erro: ('.$ex->getCode().') '.$ex->getMessage());
//return ['sucesso'=>false, 'Erro: '=>$ex->getCode().': '.$ex->getMessage()];
}
}
As the function
incluir()
is being invoked? I suggest you check the contents of the parameter$session
. In fact, if you want, you can correct your code: change$sesion
for$session
.– felipe.zkn
Include this if you give a var_dump($log) in the line before $this->em->persist($log) it comes back filled in. But if you put the var_dump right after the flush turn NULL.
– Joao Nivaldo
The
$em
is well defined?– felipe.zkn
According to the documentation, you are to invoke in the correct order.
– felipe.zkn
I would check if the Entitymanager object is as it is due -- that is, if the connection is configured --, if the data is coming in the session and if
$_SERVER["REMOTE_ADDR"]
is also OK.– felipe.zkn
So I have other classes using this same Entitymanager and they all work except this unfortunate one. And do not give any error.
– Joao Nivaldo
You can try to see the value of
$this->em->getUnitOfWork()->getEntityState($log);
right after the call frompersist()
?– felipe.zkn
I can not understand why you are giving problem, but I have a question to ask: why are you setting the
id
of the user andid
company, rather than make a relationshipManyToOne
with the user and the company?– Rodrigo Rigotti