php的数据持久化_PHP(collection)数据持久化操作-数据库映射

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

下面是Colletion及子类的代码:

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及子类的代码

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

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值