0

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();
}
}
PowerStat
  • 3,757
  • 8
  • 32
  • 57
  • I think its possible but to help you please show us your code you have already written till now. – TiMESPLiNTER Oct 31 '13 at 08:03
  • thank you for your comment. I added some more code, showing the two datamappers' code i mentioned. I hope this helps to clarify the issue. – Malte Behrmann Oct 31 '13 at 08:55
  • Okay and can you add a var_dump of your session variable you fetch with the Image inside? Basically I think it's okay like this. You fetch the session variable and then fetch the Image out of this variable and decode it. But I would use return values in the functions instead of passing references if I were you. – TiMESPLiNTER Oct 31 '13 at 09:10
  • At the moment i have no var_dump of my session, as i want to improve the storing and fetching of the book object with the image inside before using it. Can you explain in more detail, what you mean by "You fetch the session variable and then fetch the Image out of this variable and decode it.". Should i only use one general datamapper, which has a seperate method to deal with images or how should i structure the storage/loading of the book with image inside ? – Malte Behrmann Oct 31 '13 at 10:11
  • Something like this yes. If your book object has an image object, make a method to load the book object from session and one method to prepare your image stored in the book object. – TiMESPLiNTER Oct 31 '13 at 10:14

0 Answers0