最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

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

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

與領(lǐng)域模型相關(guān)的三個數(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)域模型(這里只建了一個Venue類用于理解)
namespace woo\domain;

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

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

//這里只建立一個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數(shù)組轉(zhuǎn)換成對象
    return new SpaceCollection($raw,$this);      //這個類的基類在下面    
  }
  
  protected function doCreateObject (array $array){  //創(chuàng)建對象
    $obj = new \woo\domain\Venue($array['id']);
    $obj->setname($array['name']);
    return $obj;
  }
  
  protected function doInsert(\woo\domain\DomainObject $object){ //將對象插入數(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(){          //返回一個sql語句對象
    return $this->selectStmt;
  }
  
}

Iterator接口定義的方法:

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

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

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();  //對象集合
  
  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();  //子類中實現(xiàn)用來在插入對象時檢查類型的
  
  protected function notifyAccess(){  //不知道干嘛的
    
  }
  
  private function getRow($num){    //獲取集合中的單條數(shù)據(jù),就是這里通過數(shù)據(jù)映射器將數(shù)據(jù)創(chuàng)建成對象
    $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");
//插入對象到數(shù)據(jù)庫
$mapper->insert($venue);
//從數(shù)據(jù)庫中讀出剛才插入的對象
$venue = $mapper->find($venue->getId());
print_r($venue);

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


//結(jié)束

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

相關(guān)文章

最新評論

天水市| 栾川县| 富宁县| 虹口区| 兴海县| 凌云县| 和田县| 临高县| 大新县| 凤山县| 井研县| 大姚县| 页游| 庄河市| 扎赉特旗| 铜梁县| 封开县| 罗定市| 绍兴县| 绥中县| 南皮县| 奇台县| 色达县| 青阳县| 元江| 旺苍县| 潜江市| 张家港市| 屏东市| 嵊泗县| 华坪县| 嘉鱼县| 大石桥市| 崇左市| 沽源县| 新宁县| 长子县| 彭州市| 读书| 光泽县| 蒲江县|