/*The identity map adds an identity mapping class based on the data mapper , and the main function is to save the objects that have already been created, and to get the degradation of the system performance directly, rather than repeating the creation, when needed. A partial invocation of the identity mapping class is also added on the basis of the data mapper , with the sample code as follows:*/namespace Woo\domain;//Identity Mapping classclassobjectwatcher{Private $all=Array();//small warehouse where objects are stored Private Static $instance;//Single Case Private function__construct () {}Static functioninstance () {if(!self::$instance) { self::$instance=NewObjectwatcher (); } returnSelf::$instance; } //gets a unique identity that uses the domain class class name +id to create a unique identity that avoids duplicate ID occurrences when multiple database tables call this class functionGlobalkey (DomainObject$obj){ $key=Get_class($obj) . "." .$obj-getId (); return $key; } //adding Objects Static functionAdd (DomainObject$obj){ $inst= Self::instance (); $inst->all[$inst->globalkey ($obj)] =$obj; } //Get Object Static functionExists$classname,$id){ $inst= Self::instance (); $key= "$classname.$id"; if(isset($inst->all[$key]){ return $inst->all[$key]; } return NULL; }}namespace Woo\mapper;Abstract classmapper{//abstract base class Abstract Static $PDO;//PDO object for manipulating databases function__construct () {if(!isset(Self::$PDO){ $dsn= \woo\base\applicationregistry::Getdsn (); if(Is_null($dsn)){ Throw New\woo\base\appexception ("No DNS"); } Self::$PDO=New\pdo ($dsn); Self::$PDO->setattribute (\pdo::attr_errmode,\pdo::errmode_exception); } } //new methods based on data mapper the following will be called New, the function is to get the object instead of querying the database and repeatedly create the object//(compare the original data mapper related code to understand) Private functionGetfromap ($id){ return\woo\domain\objectwatcher::exists ($this->targetclass (),$id); } //added, the function here is to save the created object . Private functionAddtomap (\woo\domain\domainobject$obj){////// return\woo\domain\objectwatcher::add ($obj); } //comparing the code of the original data mapper, it is found that it is not created and inserted into the identity map class by the method of creating and inserting the object in the identity mapping class first, but not the one that is called//subclass, and the following find method follows this principle functionCreateObject ($array){ $old=$this->getfrommap ($array[' ID ']);//New if($old){return $old}//New $obj=$this->docreateobject ($array);//implemented in subclasses $this->addtomap ($obj);//New return $obj; } // functionFind$id){//get a piece of data from the database and create it as an object by ID $old=$this->getfrommap ($id);//New if($old){return $old}//New $this->selectstmt ()->execute (Array($id)); $array=$this->SELECTSTMT ()fetch (); $this->SELECTSTMT ()closecursor (); if(!Is_array($array)){ return NULL; } if(!isset($array[' ID '])){ return NULL; } $object=$this->createobject ($array); $this->addtomap ($object);//New return $object; } functionInsert (\woo\domain\domainobject$obj){//inserting object data into a database $this->doinsert ($obj); $this->addtomap ($obj);//New } //abstract methods that need to be implemented in subclasses Abstract functionTargetclass ();////// Abstract functionUpdate (\woo\domain\domainobject$objet); protected Abstract functionDocreateobject (Array $array); protected Abstract functionselectstmt (); protected Abstract functionDoinsert (\woo\domain\domainobject$object);}classSpacemapperextendsMapper {//other code in the Data Mapper article has been implemented here to skip/....//The class name, which is used to generate a unique identifier in the identity mapping class protected functionTargetclass () {return"Woo\\domain\\space"; }}
PHP Object-oriented identity mapping