• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP面向对象之领域模型+数据映射器实例图文详解

php 搞代码 3年前 (2022-01-22) 16次浏览 已收录 0个评论

本篇文章主要介绍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{      //抽象基类  abstrac<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码@网&</strong><strong>搞gaodaima代码</strong>t 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;  }  }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP面向对象之领域模型+数据映射器实例图文详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址