使用每个对象的不同序列化方法序列化简单对象图

时间:2013-10-31 07:59:23

标签: php session serialization datamapper

我目前正在处理多页表单,我在表单中填充域对象book。在表单之间我存储并加载book,方法是使用本机php函数serializeunserializebase64_encodebase64_decode从会话中序列化和反序列化它。

但是,我的book对象包含域对象Image,该对象也在多页表单中填充。此Image对象需要一种不同的存储方式,因为图像文件应保存到文件系统而不是对象中。

所以目前使用php mvc语言我有两个不同的数据处理器。一个storefetch book来自/ Image会话可以没有store对象而另一个可以fetchImage只有book 1}}来自/来自会话的对象。我现在的问题是,是否可以将这两个数据处理器合并为一般数据处理器,它能够检索包含Image对象的完整Image对象?

预先感谢您的帮助,如果您需要有关数据处理器的更多详细信息,请与我们联系。

编辑:添加了我所说的两个数据映射器的代码。要保存除Image域对象之外的任何其他对象,我只使用Session Datamapper。对于DBSession对象,我使用Image Session Datamapper将Image保存在文件系统中,然后使用Session Datamapper将对象存储到session。注释SessionHandlerInterface是我的自定义SessionHandler,它实现 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); } } 以将Sessions保存到数据库表。 我希望我发布的代码不多。

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();
}
}

Session DataMapper

{{1}}

0 个答案:

没有答案