美文网首页
PHP-“mapper”数据持久化操作-数据库映射

PHP-“mapper”数据持久化操作-数据库映射

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

对象间的组织关系和关系数据库中的表是不通的。设计一个独立的类来负责在领域模型中隐藏数据库操作并管理数据转换中不可避免的冲突
<?php
namespace woo\mapper;
using \woo\base\ApplicationRegistry;

abstract class Mapper{
protected static PDO; function __construct(){ if(! isset(self::PDO)){
dsn = \woo\base\ApplicationRegistry::getDSN(); if( is_null(dsn)){
// throw new \woo\base\AppException("no dsn");
}
}
self::PDO = new \PDO(dsn);
self::PDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } function find(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 createObject(array){
obj =this->doCreateObject(array); returnobj;
}
function insert(\woo\domain\DomainObject obj){this->doInsert(obj); } abstract function update(\woo\domain\DomainObjectobj);
protected abstract function doCreateObject(array array); protected abstract function doInsert(\woo\domain\DomainObjectobject);
protected abstract function selectStmt();
}

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 values(?)");
}
function getCollection(array row) { return new SpaceCollection(raw, this); } protected function doCreateObject(arrayarray){
obj = new \woo\domain\Venue(array[id]);obj->setname(array['name']); returnobj;
}
protected function
}

相关文章

网友评论

      本文标题:PHP-“mapper”数据持久化操作-数据库映射

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