/** * {@inheritdoc} */ public function write($user, $remember = false) { if ($token = $this->getToken()) { $this->connection->delete($this->config['table'], ['id' => sha1($token)]); } $id = $this->random->generateString(64); $this->cookie->set($this->config['cookie']['name'], $id, $this->config['cookie']['lifetime'] + time()); $this->createTable(); $this->connection->insert($this->config['table'], ['id' => sha1($id), 'user_id' => $user, 'access' => date('Y-m-d H:i:s'), 'status' => $remember ? self::STATUS_REMEMBERED : self::STATUS_ACTIVE, 'data' => json_encode(['ip' => $this->getRequest()->getClientIp(), 'user-agent' => $this->getRequest()->headers->get('User-Agent')])]); }
/** * Saves an entity. * * @param object $entity * @param array $data */ public function save($entity, array $data = []) { $metadata = $this->getMetadata($entity); $identifier = $metadata->getIdentifier(true); $metadata->setValues($entity, $data, false, true); $this->trigger(Events::SAVING, $metadata, [$entity, $data]); if (!($id = $metadata->getValue($entity, $identifier, true))) { $this->trigger(Events::CREATING, $metadata, [$entity, $data]); $this->connection->insert($metadata->getTable(), $metadata->getValues($entity, true, true)); $metadata->setValue($entity, $identifier, $this->connection->lastInsertId(), true, true); $this->trigger(Events::CREATED, $metadata, [$entity, $data]); } else { $this->trigger(Events::UPDATING, $metadata, [$entity, $data]); $this->connection->update($metadata->getTable(), $metadata->getValues($entity, true, true), [$identifier => $id]); $this->trigger(Events::UPDATED, $metadata, [$entity, $data]); } $this->trigger(Events::SAVED, $metadata, [$entity, $data]); }
/** * {@inheritdoc} */ public function write($id, $data) { try { $params = ['id' => $id, 'data' => base64_encode($data), 'time' => date('Y-m-d H:i:s')]; if (null !== ($sql = $this->getMergeSql())) { $this->connection->executeQuery($sql, $params); return true; } $this->connection->beginTransaction(); try { $this->connection->delete($this->table, ['id' => $id]); $this->connection->insert($this->table, $params); $this->connection->commit(); } catch (ConnectionException $e) { $this->connection->rollback(); throw $e; } } catch (\PDOException $e) { throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e); } return true; }