当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP面向对象之领域模型+数据映射器实例(分析)

PHP面向对象之领域模型+数据映射器实例(分析)

2017年12月12日  | 移动技术网IT编程  | 我要评论

这里要说明一下 因为本人比较懒 博客中相关文章的内容更多的是对<深入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面向对象之领域模型+数据映射器实例(分析)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网