副问题[/!--empirenews.page--]
这里要声名一下 由于本人较量懒 博客中相干文章的内容更多的是对<深入PHP面向工具、模式与实践>一书中代码的清算和简朴注解利便本身日后温习和参考,
对相干内容感乐趣的初学的伴侣提议请先阅读原文。此处的内容只能当成一种进修的增补和参考。感谢!
因原书中规模模子+数据映射器的示例代码是连贯在一路的 以是这里就清算在一路了。
简朴先容一下我的观点,从数据库操纵的角度看规模模子首要是操纵数据表中的单笔记录的而数据映射器是操纵整个数据表的数据的。
按原文的表明数据映射器是一个认真将数据库数据映射到工具的类,而规模模子象征着真实天下里项目中的各个参加者,它在数据中凡是示意为一笔记录。
空话不多说,代码和注解如下:
与规模模子相干的三个数据表布局别离为venue(场合)、space(空间)、event(变乱)。
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('woodomainspace'); //这里应该证明白我上述的揣摩
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 woomapper;
abstract class Mapper{ //抽象基类
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);
}
}
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(woodomainDomainObject $obj){ //将工具数据插入数据库
$this->doInsert($obj);
}
//必要在子类中实现的各抽象要领
abstract function update(woodomainDomainObject $objet);
protected abstract function doCreateObject(array $array);
protected abstract function selectStmt();
protected abstract function doInsert(woodomainDomainObject $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 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(){ //返回一个sql语句工具
return $this->selectStmt;
}
}
Iterator接口界说的要领:
rewind() 指向列表开头
current() 返回当前指针处的元素
key() 返回当前的键(好比,指针的指)
next()
valid()
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|