美文网首页
PHP(collection)数据持久化操作-数据库映射

PHP(collection)数据持久化操作-数据库映射

作者: 美雨知春 | 来源:发表于2020-09-09 17:25 被阅读0次

数据库到客户端的数据映射,经过数据库底层(mapper),数据对象层(Collection)和领域(Domain),其中Domain是Collection的数据对象。当Mapper中的findAll获取到数据的时候,直接付给collection的构造函数raw,当用户需要使用数据时,再把raw实例化成Domain对象,本篇文章(collection)+上一篇文章(Mapper)是数据持久化操作的基础。
下面是Colletion及子类的代码:

<?php
namespace woo\mapper;
abstract class Collection implements \Iterator{
    protected $mapper;
    protected $total = 0;
    protected $raw = array();
    
    private $result;
    private $pointer = 0;
    private $objects = array();
    
    function __construct(array $raw=null, Mapper $mapper=null){
        if(!is_null($raw) && !is_null($mapper))
        {
            $this->row = $raw;
           $this->total = count($raw);
        }
        $this->mapper = $mapper;
    }
    function add(\woo\domain\DomainObject $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->row[$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 VenueCollection extends Collection implements \woo\domain\VenueCollection{
    
    function targetClass(){
        return "\woo\domain\Venue";
    }
}
class SpaceCollection extends Collection implements \woo\domain\SpaceCollection{
    
    function targetClass(){
        return "\woo\domain\Space";
    }
}

下面是Domain及子类的代码

<?php
namespace woo\domain;
abstract class DomainObject{
    private $id;
    function __construct($id = null){
        $this->id = $id;
    }
    function getId(){
        return $this->id;
    }
    static function getCollection($type){
      return array();//dummy  
    }
    function collection(){
        return self::getCollection(get_class($this));
    }
}
class Venue extends DomainObject{
    private $name;
    private $space;
    function __construct($id=null,$name=null){
        $this->name = $name;
        $this->spaces = self::getCollection("\\woo\\domain\\\Space");
        parent::__construct($id);
    }
    function setSpaces(SpaceCollection $spaces){
        $this->spaces = $spaces;
    }
    function getSpaces(){
        return $this->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;
    }
}
class Space extends DomainObject{
    private $name;
   // private $space;
    function __construct($id=null,$name=null){
        $this->name = $name;
       // $this->spaces = self::getCollection("\\woo\\domain\\\Space");
        parent::__construct($id);
    }
    function setName($name_s){
        $this->name = $name_s;
        $this->markDirty();
    }
    function getName(){
        return $this->name;
    }
}
interface VenueCollection extends \Iterator{
    function add(DomainObject $venue);
}
interface SpaceCollection extends \Iterator{
    function add(DomainObject $space);
}
interface EventCollection extends \Iterator{
    function add(DomainObject $event);
}

如果有任何问题,欢迎私信联系

相关文章

网友评论

      本文标题:PHP(collection)数据持久化操作-数据库映射

      本文链接:https://www.haomeiwen.com/subject/swtyektx.html