Mam taki kod (niedokończony ale odczytuje dane z bazy i zapisuje).
<?php
require_once('Core/Cube/Session/SessionHandler.interface.php');
require_once('Core/Cube/Session/SessionException.class.php');
require_once('Core/Cube/Database/' . Config::$DbType . '/' . Config::$DbType . '.class.php');
class CubeSession implements SessionHandler
{
private $sSessionValues = null;
private $sAddressIp = null;
private $oConnection = null;
private $sLogin = null;
public function __construct($oRegistry, $sConnectionName)
{
$this->oConnection = $oRegistry->get($sConnectionName);
session_set_save_handler(array(&$this, 'open'), array(&$this, 'close'), array(&$this, 'read'), array(&$this, 'write'), array(&$this, 'destroy'), array(&$this, 'gc'));
$this->sAddressIp = $_SERVER['REMOTE_ADDR'];
if (isset($_SESSION['Login'])) $this->sLogin = $_SESSION['Login']; }
public function open($sSessionSavePath, $sSessionName)
{
return true;
}
public function close()
{
$this->gc(Config::$SessionMaxLifeTime);
return true;
}
public function read($sSessionId)
{
$this->oConnection->query('SELECT sessions_values FROM sessions WHERE sessions_identifier = '' . $sSessionId . ''');
$this->sSessionValues = $this->oConnection->fetchOne();
return $this->sSessionValues;
}
public function write($sSessionId, $aSessionValues)
{
$aInsert = array('sessions_identifier'=>session_id
(), 'sessions_time_start'=>'21:15', 'sessions_last_time' => '21:16', 'sessions_values'=>'serialize','login'=>'dupa', 'address_ip'=>$this->sAddressIp );
@$this->oConnection->insert('sessions', $aInsert);
echo '<br/>Metoda write() została wywołana<br/>'; }
public function destroy($sSessionId){}
public function gc($iSessionLifeTime)
{
}
}
?>
Błąd, który podałem w temacie pojawia się gdy tabela sessions wygląda tak:
CREATE TABLE sessions
(
sessions_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
sessions_identifier VARCHAR(32) NOT NULL,
sessions_time_start TIME NOT NULL,
sessions_last_time TIME NOT NULL,
sessions_values TEXT NOT NULL,
login VARCHAR(20),
address_ip VARCHAR(15),
UNIQUE(sessions_identifier)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
Jeśli usunę UNIQUE(sessions_identifier) z tabeli to błąd się nie pojawia. Można to obejść sprawdzając czy wpis o podanym numerze sesji istnieje. Jeśli nie to wykonujemy insert a jeśli istnieje to update.
Problem w tym, że zawsze sprawdzałem to poprzez mysql_affected_rows(). Niestety w tym wypadku to nic nie daje i błąd się pojawia. Mógłbym wykonywać selecta i sprawdzać czy został zwrócony wynik (jeśli tak to wpis już istnieje) ale chciałbym uniknąć zbędnego zapytania do bazy..
Macie jakiś pomysł?