現在、複数ページのフォームに取り組んでおり、book
フォーム中にドメイン オブジェクトを設定しています。フォームの合間に、ネイティブ php 関数、、およびbook
を使用して、セッションからシリアル化および非シリアル化することにより、 を保存およびロードします。serialize
unserialize
base64_encode
base64_decode
ただし、私のbook
オブジェクトにはドメイン Object が含まれておりImage
、これは複数ページのフォーム中にも入力されます。画像ファイルはオブジェクトではなくファイルシステムに保存する必要があるため、このImage
オブジェクトには別の方法でセッションを保存する必要があります。
そのため、現時点では php mvc 言語を使用して、2 つの異なるデータマッパーを使用しています。1 つはオブジェクトなしでセッションstore
とfetch
の間で行うことができ、もう 1 つはセッションとの間でオブジェクトのみを行うことができます。私の質問は、この 2 つのデータマッパーをobject を含む完全なオブジェクトを取得できる一般的なデータマッパーにマージすることは可能ですか?book
Image
store
fetch
Image
book
Image
データマッパーに関する詳細が必要な場合はお知らせください。
編集:私が話した2つのデータマッパーのコードを追加しました。ドメインオブジェクト以外のオブジェクトを保存するImage
には、セッションデータマッパーのみを使用します。オブジェクトについてはImage
、Image Session Datamapper を使用してイメージをファイルシステムに保存し、次に Session Datamapper を使用してオブジェクトをセッションに保存します。注意として、セッションをデータベーステーブルに保存するためにDBSession
実装するカスタム SessionHandler があります。SessionHandlerInterface
あまり多くのコードを投稿しないことを願っています。
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);
}
}
セッション データマッパー
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();
}
}