设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2021-01-24 08:45 所属栏目:121 来源:网络整理
导读: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

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);
}
}

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

//新增标记的对象
private function addToMap(\woo\domain\DomainObject $obj){//////
return \woo\domain\ObjectWatcher::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(\woo\domain\DomainObject $obj){ //将对象数据插入数据库
$this->doInsert($obj);
$this->addToMap($obj);
}

//需要在子类中实现的各个抽象方法
abstract function targetClass(); //获取类的类型
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); //插入操作

}

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 \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(){ //返回一个预处理对象
return $this->selectStmt;
}

}

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

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

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读