I am currently working on a multi-page form, where I am populating a domain object book
during the forms. In between the forms I store and load the book
by serializing and unserializing it from session using the native php functions serialize
, unserialize
, base64_encode
and base64_decode
.
However my book
object contains a domain Object Image
, which is also populated during the multi-page form. This Image
object needs a different way of storage to session, as the image file should be saved to the filesystem and not in the object.
So at the moment using php mvc language I have two different datamappers. One can store
and fetch
a book
to/from session without the Image
object and the other can store
and fetch
only the Image
object to/from session. My question is now, is it possible to merge this two datamappers to a general one, which is able to retrieve the full book
object including Image
object ?
Thanks in advance for your help and if you need more details concerning the datamappers let me know.
EDIT: Added the code of the two datamapper I have spoken of. To save any other object than the Image
domain Object I only use the Session Datamapper. For the Image
object I use the Image Session Datamapper to save the Image in the filesystem and then the Session Datamapper to store the object to session. As a note DBSession
is my custom SessionHandler implementing SessionHandlerInterface
to save Sessions to a database table.
I hope i posted not too much code.
Image Session DataMapper;
namespace model\DataAccess\DataMapper;
class tmpImage {
protected $dbhandler;
public function __construct ($dbhandler) {
$this->dbhandler = $dbhandler;
}
protected function createLocalFilestring (\model\DomainObjects\Image $Image) {
$file = ROOT."data/temp/".$Image->getType()."/".$Image->getName().$Image->getExtension();
return str_replace("\\","/",$file);
}
protected function createHTTPFilestring (\model\DomainObjects\Image $Image) {
$file = HTTP."data/temp/".$Image->getType()."/".$Image->getName().$Image->getExtension();
return str_replace("\\","/",$file);
}
public function store (\model\DomainObjects\Image $Image) {
$filestring=$this->createLocalFilestring($Image);
move_uploaded_file($Image->getTmpname(),$filestring);
}
public function fetch (\model\DomainObjects\Image $Image) {
$filestring=$this->createHTTPFilestring($Image);
$Image->setUrl($filestring);
}
public function remove (\model\DomainObjects\Image $Image) {
$filestring=$this->createLocalFilestring($Image);
unlink($filestring);
}
}
Session DataMapper
namespace model\DataAccess\DataMapper;
class sessionMapper {
private $dbhandler;
private $sessionHandler;
private $cache=array();
public function __construct($dbhandler){
$this->dbhandler = $dbhandler;
$this->sessionHandler = DBSession::start($this->dbhandler);
$this->loadCache();
}
public function fetch (&$varToFill,$refName){
//check if variable with $refName exists in Session
//if existant read out of cache
if(is_object($varToFill)===true){
if($this->ExistInSession($refName)===true) {
$varToFill = unserialize(base64_decode($this->cache[$refName]));
}
//else object remains empty
}
//variable is not an object
else {
if($this->ExistInSession($refName)===true) {
$varToFill = $this->cache[$refName];
}
//else retrieve null
else {
$varToFill = null;
}
}
}
public function store ($varToFill,$refName){
if(is_object($varToFill)===true){
$this->cache[$refName] = base64_encode(serialize($varToFill));
}
else {
$this->cache[$refName] = $varToFill;
}
//save cache to Session
$this->saveCache($refName);
}
public function remove ($refName) {
//remove from cache
unset($this->cache[$refName]);
//remove from session
unset($_SESSION[$refName]);
}
//this function reads all stored session data and writes it into cache
private function loadCache () {
$this->cache = $_SESSION;
}
private function saveCache ($refName) {
$_SESSION[$refName] = $this->cache[$refName];
//for some reason direct accessing the session handler leads to writing an empty data field at session closing to the database
//so you call this function and the right data is inserted into DB but when Session is closed and PHP tries to write SessionData to DB
//before it is lost it retrieves an empty data field it writes to DB which overwrites your prior data
//$this->sessionHandler->write(session_id(),$this->cache[$refName]);
}
//this function checks if SessionVariable with given name exists
public function ExistInSession ($name) {
if((isset($this->cache[$name]))&&(!empty($this->cache[$name]))){
return true;
}
return false;
}
public function getSessionID () {
return session_id();
}
}