| 
                         下面这个类是处理多行记录的,传递数据库中取出的原始数据和映射器进去,然后通过数据映射器在获取数据时将其创建成对象
 
private $result; 
private $pointer = 0;    //指针 
private $objects = array();  //对象集合
function __construct (array $raw = null,Mapper $mapper= null){ 
if(!is_null($raw)&& !is_null($mapper)){ 
$this->raw = $raw; 
$this->total = count($raw); 
} 
$this->mapper = $mapper; 
} 
function add(woodomainDmainObject $object){  //这里是直接添加对象 
$class = $this->targetClass(); 
if(!($object instanceof $class)){ 
throw new Exception("This is a {$class} collection"); 
} 
$this->notifyAccess(); 
$this->objects[$this->total] = $object; 
$this->total ++; 
} 
abstract function targetClass();  //子类中实现用来在插入对象时检查类型的 
protected function notifyAccess(){  //不知道干嘛的 
} 
private function getRow($num){    //获取集合中的单条数据,就是这里通过数据映射器将数据创建成对象 
$this->notifyAccess(); 
if($num >= $this->total || $num < 0){ 
return null; 
} 
if(isset($this->objects[$num]){ 
return $this->objects[$num]; 
} 
if(isset($this->raw[$num]){ 
$this->objects[$num] = $this->mapper->createObject($this->raw[$num]); 
return $this->objects[$num]; 
} 
} 
public function rewind(){      //重置指针 
$this->pointer = 0; 
} 
public function current(){      //获取当前指针对象 
return $this->getRow($this->pointer); 
} 
public function key(){        //获取当前指针 
return $this->pointer; 
} 
public function next(){      //获取当前指针对象,并将指针下移 
$row = $this->getRow($this->pointer); 
if($row){$this->pointer ++} 
return $row; 
} 
public function valid(){    //验证 
return (!is_null($this->current())); 
} 
} 
//子类 
class VenueColletion extends Collection implements woodomainVenueCollection{ 
function targetClass(){ 
return "woodomainVenue"; 
} 
} 
//客户端 
$mapper = new woomapperVenueMapper(); 
$venue = $mapper->find(12); 
print_r($venue); 
$venue = new woodomainVenue(); 
$venue->setName("the likey lounge-yy"); 
//插入对象到数据库 
$mapper->insert($venue); 
//从数据库中读出刚才插入的对象 
$venue = $mapper->find($venue->getId()); 
print_r($venue); 
//修改对象 
$venue->setName("the bibble beer likey lounge-yy"); 
//调用update来更新记录 
$mapper->update($venue); 
//再次读出对象数据 
$venue = $mapper->find($venue->getId()); 
print_r($venue); 
//结束 
以上这篇PHP面向对象之领域模型+数据映射器实例(分析)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。                         (编辑:泰州站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |