国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP面向對象之領域模型+數據映射器實例(分析)

2024-05-04 23:58:11
字體:
來源:轉載
供稿:網友

這里要說明一下 因為本人比較懶 博客中相關文章的內容更多的是對<深入PHP面向對象、模式與實踐>一書中代碼的整理和簡單注解方便自己日后復習和參考,

對相關內容感興趣的初學的朋友建議請先閱讀原文。此處的內容只能當成一種學習的補充和參考。謝謝!

因原書中領域模型+數據映射器的示例代碼是連貫在一起的 所以這里就整理在一起了。

簡單介紹一下我的看法,從數據庫操作的角度看領域模型主要是操作數據表中的單條記錄的而數據映射器是操作整個數據表的數據的。

按原文的解釋數據映射器是一個負責將數據庫數據映射到對象的類,而領域模型象征著真實世界里項目中的各個參與者,它在數據中通常表現為一條記錄。

廢話不多說,代碼和注解如下:

與領域模型相關的三個數據表結構分別為venue(場所)、space(空間)、event(事件)。

create table 'venue' (   'id' int(11) not null auto_increment,   'name' text,   primary key ('id'))create table 'space' (   'id' int(11) not null auto_increment,   'venue' int(11) default null,   'name' text,   primary key ('id'))create table 'event' (   'id' int(11) not null auto_increment,   'space' int(11) default null,   'start' mediumtext,   'duration' int(11) default null,   'name' text,   primary key ('id'))
//領域模型(這里只建了一個Venue類用于理解)namespace woo/domain;abstract class DomainObject{      //抽象基類    private $id;    function __construct ($id=null){    $this->id = $id;  }    function getId(){    return $this->id;  }    //原書沒有具體實現,應該是用于獲取對象的從屬對象的,比如venue(場所)相關的space(空間)對象  //具體的代碼實現中應該從數據庫中查詢了相關數據并調用了Collection類,下面看到這個類的時候會有一個了解  //而且這個方法的實現應該放在子類中才對  static function getCollection($type){       return array();  }    function collection(){    return self::getCollection(get_class($this));  }  }class Venue extends DomainObject {  private $name;  private $spaces;    function __construct ($id = null,$name=null){    $this->name= $name;    $this->spaces = self::getCollection('//woo//domain//space'); //這里應該證明了我上述的猜測    parent::__construct($id);  }    function setSpaces(SpaceCollection $spaces){    $this->spaces = $spaces;  }    function addSpace(Space $space){    $this->spaces->add($space);    $space->setVenue($this);  }    function setName($name_s){    $this->name = $name_s;    $this->markDirty();  }    function getName(){    return $this->name;  }}//數據映射器(正如原文的解釋數據映射器是一個負責將數據庫數據映射到對象的類)namespace woo/mapper;abstract class Mapper{      //抽象基類  abstract static $PDO;    //操作數據庫的pdo對象  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);    }  }    function createObject($array){          //將數組創建為上述領域模型中的對象    $obj = $this->doCreateObject($array);    //在子類中實現    return $obj;  }    function find($id){                //通過ID從數據庫中獲取一條數據并創建為對象    $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);    return $object;    }    function insert(/woo/domain/DomainObject $obj){      //將對象數據插入數據庫    $this->doInsert($obj);  }    //需要在子類中實現的各抽象方法  abstract function update(/woo/domain/DomainObject $objet);  protected abstract function doCreateObject(array $array);  protected abstract function selectStmt();  protected abstract function doInsert(/woo/domain/DomainObject $object);}//這里只建立一個VenueMapper類用于理解class VenueMapper extends Mapper {  function __construct (){        parent::__construct();  //各種sql語句對象      $this->selectStmt = self::$PDO->prepare("select * from venue where id=?");    $this->updateStmt = self::$PDO->prepare("update venue set name=?,id=? where id=?");    $this->insertStmt = self::$PDO->prepare("insert into venue (name) values(?)");  }    protected function getCollection(array $raw){    //將Space數組轉換成對象    return new SpaceCollection($raw,$this);      //這個類的基類在下面      }    protected function doCreateObject (array $array){  //創建對象    $obj = new /woo/domain/Venue($array['id']);    $obj->setname($array['name']);    return $obj;  }    protected function doInsert(/woo/domain/DomainObject $object){ //將對象插入數據庫    print 'inserting';    debug_print_backtrace();    $values = array($object->getName());    $this->insertStmt->execute($values);    $id = self::$PDO->lastInsertId();    $object->setId($id);  }    function update(/woo/domain/DomainObject $object){    //修改數據庫數據    print "updation/n";    $values = array($object->getName(),$object->getId(),$object->getId());    $this->updateStmt->execute($values);  }    function selectStmt(){          //返回一個sql語句對象    return $this->selectStmt;  }  }

Iterator接口定義的方法:

rewind()            指向列表開頭   
current()            返回當前指針處的元素
key()                返回當前的鍵(比如,指針的指)
next()               
valid()

下面這個類是處理多行記錄的,傳遞數據庫中取出的原始數據和映射器進去,然后通過數據映射器在獲取數據時將其創建成對象

abstract class Collection implements /Iterator{  protected $mapper;      //數據映射器  protected $total = 0;    //集合元素總數量  protected $raw = array();  //原始數據    private $result;  private $pointer = 0;    //指針  private $objects = array();  //對象集合    function __construct (array $raw = null,Mapper $mapper= null){    if(!is_null($raw)&& !is_null($mapper)){      $this->raw = $raw;      $this->total = count($raw);    }    $this->mapper = $mapper;  }    function add(/woo/domain/DmainObject $object){  //這里是直接添加對象    $class = $this->targetClass();    if(!($object instanceof $class)){      throw new Exception("This is a {$class} collection");    }    $this->notifyAccess();    $this->objects[$this->total] = $object;    $this->total ++;  }    abstract function targetClass();  //子類中實現用來在插入對象時檢查類型的    protected function notifyAccess(){  //不知道干嘛的      }    private function getRow($num){    //獲取集合中的單條數據,就是這里通過數據映射器將數據創建成對象    $this->notifyAccess();    if($num >= $this->total || $num < 0){      return null;    }    if(isset($this->objects[$num]){      return $this->objects[$num];    }    if(isset($this->raw[$num]){      $this->objects[$num] = $this->mapper->createObject($this->raw[$num]);      return $this->objects[$num];    }  }    public function rewind(){      //重置指針    $this->pointer = 0;  }    public function current(){      //獲取當前指針對象    return $this->getRow($this->pointer);  }    public function key(){        //獲取當前指針    return $this->pointer;  }    public function next(){      //獲取當前指針對象,并將指針下移      $row = $this->getRow($this->pointer);    if($row){$this->pointer ++}    return $row;  }    public function valid(){    //驗證    return (!is_null($this->current()));  }  }//子類class VenueColletion extends Collection implements /woo/domain/VenueCollection{  function targetClass(){    return "/woo/domain/Venue";  }}//客戶端$mapper = new /woo/mapper/VenueMapper();$venue = $mapper->find(12);print_r($venue);$venue = new /woo/domain/Venue();$venue->setName("the likey lounge-yy");//插入對象到數據庫$mapper->insert($venue);//從數據庫中讀出剛才插入的對象$venue = $mapper->find($venue->getId());print_r($venue);//修改對象$venue->setName("the bibble beer likey lounge-yy");//調用update來更新記錄$mapper->update($venue);//再次讀出對象數據$venue = $mapper->find($venue->getId());print_r($venue);//結束

以上這篇PHP面向對象之領域模型+數據映射器實例(分析)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。

 

注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 老河口市| 庐江县| 齐齐哈尔市| 车致| 正阳县| 惠水县| 临城县| 台中县| 镇雄县| 汾西县| 古交市| 邵武市| 九龙坡区| 龙江县| 台州市| 五台县| 修水县| 电白县| 永康市| 同心县| 奎屯市| 江川县| 冕宁县| 中西区| 新化县| 泾川县| 县级市| 调兵山市| 阜城县| 保定市| 尖扎县| 墨竹工卡县| 酉阳| 将乐县| 项城市| 偃师市| 定日县| 鞍山市| 亳州市| 平和县| 金乡县|