/** * 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; }