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 onFlush is called, all changes are already calculated and you need to refresh them if you change your entity or create a new entity.
$em = $this->getEntityManager();
$uow = $em->getUnitOfWork();
$logMetadata = $em->getClassMetadata('Your\LogClass');
...
$em->persist($log);
$uow->computeChangeSet($logMetadata, $log);
For postPersist:
$em = $this->getEntityManager();
$uow = $em->getUnitOfWork();
$log = new Log;
...
$logMetadata = $em->getClassMetadata('Your\LogClass');
$className = $logMetadata->name;
$persister = $this->getEntityPersister($className);
$persister->addInsert($log);
$uow->computeChangeSet($classMeta, $logEntry);
$postInsertIds = $persister->executeInserts();
if ($postInsertIds) {
    foreach ($postInsertIds as $id => $entity) {
        $idField = $logMetadata->identifier[0];
        $logMetadata->reflFields[$idField]->setValue($entity, $id);
        $this->addToIdentityMap($entity);
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With