PHP領域模型和資料對應器詳解

來源:互聯網
上載者:User
本文主要和大家分享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);//結束

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.