亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP面向?qū)ο笾I(lǐng)域模型+數(shù)據(jù)映射器實(shí)例(分析)

 更新時(shí)間:2017年06月21日 09:36:35   投稿:jingxian  
下面小編就為大家?guī)硪黄狿HP面向?qū)ο笾I(lǐng)域模型+數(shù)據(jù)映射器實(shí)例(分析)。小編覺得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

這里要說明一下 因?yàn)楸救吮容^懶 博客中相關(guān)文章的內(nèi)容更多的是對(duì)<深入PHP面向?qū)ο?、模式與實(shí)踐>一書中代碼的整理和簡單注解方便自己日后復(fù)習(xí)和參考,

對(duì)相關(guān)內(nèi)容感興趣的初學(xué)的朋友建議請(qǐng)先閱讀原文。此處的內(nèi)容只能當(dāng)成一種學(xué)習(xí)的補(bǔ)充和參考。謝謝!

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

簡單介紹一下我的看法,從數(shù)據(jù)庫操作的角度看領(lǐng)域模型主要是操作數(shù)據(jù)表中的單條記錄的而數(shù)據(jù)映射器是操作整個(gè)數(shù)據(jù)表的數(shù)據(jù)的。

按原文的解釋數(shù)據(jù)映射器是一個(gè)負(fù)責(zé)將數(shù)據(jù)庫數(shù)據(jù)映射到對(duì)象的類,而領(lǐng)域模型象征著真實(shí)世界里項(xiàng)目中的各個(gè)參與者,它在數(shù)據(jù)中通常表現(xiàn)為一條記錄。

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

與領(lǐng)域模型相關(guān)的三個(gè)數(shù)據(jù)表結(jié)構(gòu)分別為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')
)
//領(lǐng)域模型(這里只建了一個(gè)Venue類用于理解)
namespace woo\domain;

abstract class DomainObject{      //抽象基類
  
  private $id;
  
  function __construct ($id=null){
    $this->id = $id;
  }
  
  function getId(){
    return $this->id;
  }
  
  //原書沒有具體實(shí)現(xiàn),應(yīng)該是用于獲取對(duì)象的從屬對(duì)象的,比如venue(場所)相關(guān)的space(空間)對(duì)象
  //具體的代碼實(shí)現(xiàn)中應(yīng)該從數(shù)據(jù)庫中查詢了相關(guān)數(shù)據(jù)并調(diào)用了Collection類,下面看到這個(gè)類的時(shí)候會(huì)有一個(gè)了解
  //而且這個(gè)方法的實(shí)現(xiàn)應(yīng)該放在子類中才對(duì)
  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'); //這里應(yīng)該證明了我上述的猜測(cè)
    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;
  }
}


//數(shù)據(jù)映射器(正如原文的解釋數(shù)據(jù)映射器是一個(gè)負(fù)責(zé)將數(shù)據(jù)庫數(shù)據(jù)映射到對(duì)象的類)
namespace woo\mapper;

abstract class Mapper{      //抽象基類
  abstract static $PDO;    //操作數(shù)據(jù)庫的pdo對(duì)象
  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){          //將數(shù)組創(chuàng)建為上述領(lǐng)域模型中的對(duì)象
    $obj = $this->doCreateObject($array);    //在子類中實(shí)現(xiàn)
    return $obj;
  }
  
  function find($id){                //通過ID從數(shù)據(jù)庫中獲取一條數(shù)據(jù)并創(chuàng)建為對(duì)象
    $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){      //將對(duì)象數(shù)據(jù)插入數(shù)據(jù)庫
    $this->doInsert($obj);
  }
  
  //需要在子類中實(shí)現(xiàn)的各抽象方法
  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);
}

//這里只建立一個(gè)VenueMapper類用于理解
class VenueMapper extends Mapper {
  function __construct (){    
    parent::__construct();  //各種sql語句對(duì)象  
    $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數(shù)組轉(zhuǎn)換成對(duì)象
    return new SpaceCollection($raw,$this);      //這個(gè)類的基類在下面    
  }
  
  protected function doCreateObject (array $array){  //創(chuàng)建對(duì)象
    $obj = new \woo\domain\Venue($array['id']);
    $obj->setname($array['name']);
    return $obj;
  }
  
  protected function doInsert(\woo\domain\DomainObject $object){ //將對(duì)象插入數(shù)據(jù)庫
    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){    //修改數(shù)據(jù)庫數(shù)據(jù)
    print "updation\n";
    $values = array($object->getName(),$object->getId(),$object->getId());
    $this->updateStmt->execute($values);
  }
  
  function selectStmt(){          //返回一個(gè)sql語句對(duì)象
    return $this->selectStmt;
  }
  
}

Iterator接口定義的方法:

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

下面這個(gè)類是處理多行記錄的,傳遞數(shù)據(jù)庫中取出的原始數(shù)據(jù)和映射器進(jìn)去,然后通過數(shù)據(jù)映射器在獲取數(shù)據(jù)時(shí)將其創(chuàng)建成對(duì)象

abstract class Collection implements \Iterator{
  protected $mapper;      //數(shù)據(jù)映射器
  protected $total = 0;    //集合元素總數(shù)量
  protected $raw = array();  //原始數(shù)據(jù)
  
  private $result;
  private $pointer = 0;    //指針
  private $objects = array();  //對(duì)象集合
  
  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){  //這里是直接添加對(duì)象
    $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();  //子類中實(shí)現(xiàn)用來在插入對(duì)象時(shí)檢查類型的
  
  protected function notifyAccess(){  //不知道干嘛的
    
  }
  
  private function getRow($num){    //獲取集合中的單條數(shù)據(jù),就是這里通過數(shù)據(jù)映射器將數(shù)據(jù)創(chuàng)建成對(duì)象
    $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(){      //獲取當(dāng)前指針對(duì)象
    return $this->getRow($this->pointer);
  }
  
  public function key(){        //獲取當(dāng)前指針
    return $this->pointer;
  }
  
  public function next(){      //獲取當(dāng)前指針對(duì)象,并將指針下移  
    $row = $this->getRow($this->pointer);
    if($row){$this->pointer ++}
    return $row;
  }
  
  public function valid(){    //驗(yàn)證
    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");
//插入對(duì)象到數(shù)據(jù)庫
$mapper->insert($venue);
//從數(shù)據(jù)庫中讀出剛才插入的對(duì)象
$venue = $mapper->find($venue->getId());
print_r($venue);

//修改對(duì)象
$venue->setName("the bibble beer likey lounge-yy");
//調(diào)用update來更新記錄
$mapper->update($venue);
//再次讀出對(duì)象數(shù)據(jù)
$venue = $mapper->find($venue->getId());
print_r($venue);


//結(jié)束

以上這篇PHP面向?qū)ο笾I(lǐng)域模型+數(shù)據(jù)映射器實(shí)例(分析)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Laravel視圖間共享數(shù)據(jù)與視圖Composer

    詳解Laravel視圖間共享數(shù)據(jù)與視圖Composer

    視圖的基本使用很簡單,可查看視圖文檔了解詳情,今天這里我們演示兩個(gè)使用示例:在視圖間共享數(shù)據(jù)和視圖Composer。下面一起來看看。
    2016-08-08
  • 淺談php函數(shù)serialize()與unserialize()的使用方法

    淺談php函數(shù)serialize()與unserialize()的使用方法

    在php中serialize()與unserialize()函數(shù)是一對(duì)函數(shù),下面本文章就來為各位同學(xué)介紹serialize()與unserialize()函數(shù)的使用例子,希望能幫助到各位。
    2014-08-08
  • 最新版本PHP 7 vs HHVM 多角度比較

    最新版本PHP 7 vs HHVM 多角度比較

    PHP 7 是 PHP 社區(qū)對(duì) HHVM 的回應(yīng)。PHP 7 發(fā)布的預(yù)覽版本號(hào)稱比之前的 PHP 5 的性能要提升100%。不過,PHP 還有一個(gè)競爭對(duì)手 HHVM (HipHop Virtual Machine) 一個(gè)運(yùn)行 PHP 代碼的虛擬工具。二者直接的比較正在升溫,那么讓我們來看一下他們直接的性能對(duì)比吧
    2016-02-02
  • 6個(gè)超實(shí)用的PHP代碼片段

    6個(gè)超實(shí)用的PHP代碼片段

    這篇文章主要介紹了10個(gè)超實(shí)用的PHP代碼樣例:黑名單過濾、隨機(jī)顏色生成器、從網(wǎng)上下載文件、強(qiáng)制下載文件、截取圖片、檢查網(wǎng)站是否宕機(jī),需要的朋友可以參考下
    2015-08-08
  • CodeIgniter刪除和設(shè)置Cookie的方法

    CodeIgniter刪除和設(shè)置Cookie的方法

    這篇文章主要介紹了CodeIgniter刪除和設(shè)置Cookie的方法,涉及CodeIgniter操作cookie的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • ThinkPHP單字母函數(shù)(快捷方法)使用總結(jié)

    ThinkPHP單字母函數(shù)(快捷方法)使用總結(jié)

    這篇文章主要介紹了ThinkPHP單字母函數(shù)(快捷方法)使用總結(jié),對(duì)ThinkPHP的快捷方法做了針對(duì)性的歸納總結(jié),需要的朋友可以參考下
    2014-07-07
  • 測(cè)試php連接mysql是否成功的代碼分享

    測(cè)試php連接mysql是否成功的代碼分享

    很多情況下我們不知道是php錯(cuò)誤,還是mysql用戶名密碼不對(duì)導(dǎo)致cms運(yùn)行錯(cuò)誤,可以先用下面的代碼測(cè)試一下就可以了
    2014-01-01
  • php each 返回?cái)?shù)組中當(dāng)前的鍵值對(duì)并將數(shù)組指針向前移動(dòng)一步實(shí)例

    php each 返回?cái)?shù)組中當(dāng)前的鍵值對(duì)并將數(shù)組指針向前移動(dòng)一步實(shí)例

    php each函數(shù)用于獲取數(shù)組的鍵值對(duì),并將數(shù)組指針向前移動(dòng)一步, each函數(shù)經(jīng)常和list結(jié)合使用來遍歷數(shù)組。本文章向大家介紹each的基本使用方法,需要的朋友可以參考下
    2016-11-11
  • Yii2 隊(duì)列 shmilyzxt/yii2-queue 簡單概述

    Yii2 隊(duì)列 shmilyzxt/yii2-queue 簡單概述

    這篇文章主要介紹了Yii2 隊(duì)列 shmilyzxt/yii2-queue 的簡單概述,需要的朋友可以參考下
    2017-08-08
  • php中stream(流)的用法

    php中stream(流)的用法

    Stream是PHP開發(fā)里最容易被忽視的函數(shù)系列(SPL系列,Stream系列,pack函數(shù),封裝協(xié)議)之一,但其是個(gè)很有用也很重要的函數(shù)。Stream可以翻譯為“流”,下面是使用方法
    2014-03-03

最新評(píng)論