I have setup a doctrine listener which is fired on different database actions and creates a log entity and insert it into the database.
class FOO {
// [...]
public function onFlush(OnFlushEventArgs $args)
{
foreach ($args->getEntityManager()->getUnitOfWork()->getScheduledEntityUpdates() as $entity) {
$this->createLog('edit', $entity);
}
}
private function createLog($action, $entity)
{
if ($entity instanceof Log) {
return;
}
$log = new Log;
$log->setAction($action);
$log->setTime(new \DateTime);
$log->setForeignId($entity->getId());
$log->setUser($this->getUser());
$t = explode('\\', get_class($entity));
$entity_class = strtolower(end($t));
$log->setEntity($entity_class);
$this->getEntityManager()->persist($log);
}
// [...]
}
When I update an object, I get the following error:
SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
In the profiler I see this query:
INSERT INTO vvg_logs (action, entity, foreign_id, user_id, time) VALUES (?, ?, ?, ?, ?)
Parameters: { }
How could I resolve this problem?
UPDATE
Here is my new topic connecting to this question: LINK
Because when
onFlushis called, all changes are already calculated and you need to refresh them if you change your entity or create a new entity.For postPersist: