/** * (non-PHPdoc) * @see \Koldy\Cache\Driver\AbstractCacheDriver::set() */ public function set($key, $value, $seconds = null) { $this->checkKey($key); if ($seconds === null) { $seconds = $this->defaultDuration; } $update = new Update($this->config['table']); $update->setConnection($this->config['connection']); $ok = $update->set('updated_at', date('Y-m-d H:i:s'))->set('expires_at', time() + $seconds)->set('data', serialize($value))->where('id', $key)->exec(); if ($ok === false || $ok === 0 && !$this->has($key)) { $insert = new Insert($this->config['table']); $insert->setConnection($this->config['connection']); $insert->add(array('id' => $key, 'updated_at' => date('Y-m-d H:i:s'), 'expires_at' => time() + $seconds, 'data' => serialize($value))); $insert->exec(); } return true; }
/** * Save this initialized object into database. * * @return integer how many rows is affected */ public function save() { $data = $this->getData(); $originalData = $this->originalData; $toUpdate = array(); foreach ($data as $field => $value) { if (isset($originalData[$field]) || $originalData[$field] === null) { if ($value !== $originalData[$field]) { $toUpdate[$field] = $value; } } else { $toUpdate[$field] = $value; } } if (sizeof($toUpdate) > 0) { if (!is_array(static::$primaryKey)) { if (isset($originalData[static::$primaryKey])) { // we have pk value, so lets update $update = new Update(static::getTableName()); $update->setValues($toUpdate); if (!is_array(static::$primaryKey)) { $update->where(static::$primaryKey, $data[static::$primaryKey]); } else { foreach (static::$primaryKey as $field) { $update->where($field, $data[$field]); } } try { $result = $update->exec(static::$connection); } catch (Exception $e) { $result = false; } if ($result !== false) { $this->originalData = $this->data; } return $result; } else { // we don't have pk value, so lets insert $insert = new Insert(static::getTableName()); $insert->add($toUpdate); try { $insert->exec(static::$connection); } catch (Exception $e) { return false; } $this->data[static::$primaryKey] = Db::getAdapter(static::$connection)->getLastInsertId(); $this->originalData = $this->data; } } else { // TODO: Implementirati multiple key throw new Exception('Multiple primary key not implemented'); } } return true; }
/** * (non-PHPdoc) * @see SessionHandlerInterface::write() */ public function write($sessionid, $sessiondata) { $data = array('time' => time(), 'data' => $sessiondata); $sess = $this->getDbData($sessionid); if ($sess === false) { // the record doesn't exists in database, lets insert it $data['id'] = $sessionid; $insert = new Insert($this->config['table']); $insert->add($data); if ($insert->exec($this->config['connection']) === false) { Log::error('Error inserting session data into session table. Data=' . print_r($data, true)); return false; } } else { // the record data already exists in db $update = new Update($this->config['table']); $update->setValues($data)->where('id', $sessionid); if ($update->exec($this->config['connection']) === false) { Log::error("Error update session data in session table for id={$sessionid} and data=" . print_r($data, true)); return false; } } return true; }
/** * (non-PHPdoc) * @see \Koldy\Log\Writer\AbstractLogWriter::logMessage() */ protected function logMessage($level, $message) { if ($this->inserting) { return; } $data = $this->getFieldsData($level, $message); if ($data !== false) { if (in_array($level, $this->config['log'])) { $this->inserting = true; $insert = new Insert($this->config['table']); $insert->add($data); if ($insert->exec($this->config['connection']) === false) { $adapter = KoldyDb::getAdapter($this->config['connection']); // do not process this with Log::exception because it will run into recursion $this->detectEmailAlert('exception'); $this->appendMessage(date('Y-m-d H:i:sO') . "\tERROR inserting log message into database: {$adapter->getLastError()}\n\n{$adapter->getLastException()->getTraceAsString()}\n"); } } $this->inserting = false; $this->detectEmailAlert($level); $this->appendMessage(date('Y-m-d H:i:sO') . "\t" . implode("\t", array_values($data)) . "\n"); } }