加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP面向对象之工作单元(实例讲解)

发布时间:2021-01-25 04:32:47 所属栏目:PHP教程 来源:网络整理
导读:工作单元 这个模式涉及到了领域模型、数据映射器和标识映射,这里就统一进行整理和回顾了。 $venue = new woodomainVenue(null,"The Green Tree

abstract static $PDO; //操作数据库的pdo对象
function __construct (){
if(!isset(self::$PDO){
$dsn = woobaseApplicationRegistry::getDSN();
if(is_null($dsn)){
throw new woobaseAppException("no dns");
}
self::$PDO = new PDO($dsn);
self::$PDO->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
}

//获取标记的对象
private function getFroMap($id){
return woodomainObjectWatcher::exists($this->targetClass(),$id);
}

//新增标记的对象
private function addToMap(woodomainDomainObject $obj){//////
return woodomainObjectWatcher::add($obj);
}

//将数据库数据映射为对象
function createObject($array){
$old = $this->getFromMap($array['id']);
if($old){return $old;}
$obj = $this->doCreateObject($array);
$this->addToMap($obj);
$obj->markClean();
return $obj;
}

function find($id){ //通过ID从数据库中获取一条数据并创建为对象
$old = $this->getFromMap($id);
if($old){return $old}

$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);
$this->addToMap($object);          
return $object;  

}

function insert(woodomainDomainObject $obj){ //将对象数据插入数据库
$this->doInsert($obj);
$this->addToMap($obj);
}

//需要在子类中实现的各个抽象方法
abstract function targetClass(); //获取类的类型
abstract function update(woodomainDomainObject $objet); //修改操作
protected abstract function doCreateObject(array $array); //创建对象
protected abstract function selectStmt(); //查询操作
protected abstract function doInsert(woodomainDomainObject $object); //插入操作

}

class VenueMapper extends Mapper {
function construct (){
parent::construct();
//预处理对象
$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 woodomainVenue($array['id']);
$obj->setname($array['name']);
return $obj;
}

protected function doInsert(woodomainDomainObject $object){ //将对象插入数据库
print 'inserting';
debug_print_backtrace();
$values = array($object->getName());
$this->insertStmt->execute($values);
$id = self::$PDO->lastInsertId();
$object->setId($id);
}

function update(woodomainDomainObject $object){ //修改数据库数据
print "updationn";
$values = array($object->getName(),$object->getId(),$object->getId());
$this->updateStmt->execute($values);
}

function selectStmt(){ //返回一个预处理对象
return $this->selectStmt;
}

}

//客户端
$venue = new woodomainVenue(null,"The Green Tree"); //在初始化时就被标记为新增对象了
$venue->addSpace(new woodomainSpace(null,"The Space Upstairs")); //这二行addSpace方法因为venue已经被标记新增所以不会再标记为修改对象,但是space在初始化的时候会被标记为新增对象
$venue->addSpace(new woodomainSpace(null,"The Bar Stage"));      
woodomainObjectWatcher::instance()->performOperations(); //与数据库交互新增一条Venue数据,以及二条space数据

以上这篇PHP面向对象之工作单元(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读