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面向工具之事变单位(实例讲授)就是小编分享给各人的所有内容了,但愿能给各人一个参考,也但愿各人多多支持编程之家。 (编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|