php框架Yaf PDO封装类和ORM集成,yaf本身不带orm数据封装,我们可以建立在pdo上实现自己的封装。
一:首先我们使用php的单例模式,建立基类。libraryBase.php
class Base {
/**
* 设计模式之单例模式
* $_instance必须声明为静态的私有变量
*/
//保存例实例在此属性中
private static $_instance;
//单例方法
public static function getInstance() {
$class_name = get_called_class();
if (!isset(self::$_instance[$class_name])) {
self::$_instance[$class_name] = new $class_name;
}
/*@var $var $class_name*/
return self::$_instance[$class_name];
}
//阻止用户复制对象实例
public function __clone() {
trigger_error('Clone is not allow', E_USER_ERROR);
}
}
二:以注册用户信息为例,$userInfo为表单传入的数据,我们要插入到数据库
我们建立了userModel类
class userModel extends Model {
}
$uid = userModel::getInstance()->add($userInfo);
我们用add方法插入数据,我们需要建立这个方法在Model中
三:ORM 集成到libraryModel.php
class Model extends Base {
//主键名称
public $primaryKey = 'id';
public $orderKey = 'id';
//数据表字段 字段名 => 字段初始值
public $fileds = array();
//只写缓存
protected $onlyCache = false;
//数据表名称
protected $table;
protected $readDb = false;
protected $cacheObj;
protected $dbObj;
function __construct() {
$this->init();
}
public function init() {
$this->table = str_replace('Model', '', get_called_class());
$this->dbObj = Db_Pdo::getInstance();
//导入db配置
$config = Common::getConfig('database');
$this->dbObj->loadConfig($config);
}
/**
* 新增数据
* @param type $parmas
* @return type
*/
public function add($parmas) {
if (!$parmas || !is_array($parmas))
return false;
$parmas = $this->initFields($parmas);
if (!$parmas)
return false;
$time = date("Y-m-d H:i:s");
$parmas['create_time'] = $time;
$parmas['update_time'] = $time;
$id = $this->insertDB($parmas);
if (!$id) {
return false;
}
return $id;
}
}
/**
* db新增
* @param type $parmas
* @return boolean
*/
private function insertDB($parmas) {
if ($this->onlyCache) {
return true;
}
if (!$parmas || !is_array($parmas)) {
return false;
}
$ret = $this->dbObj->insert($this->table, $parmas);
return $ret;
}
我们初始化了db配置,引用pdo操作类,在add方法中执行力pdo数据insertDB操作
四:最后我们要在pdo操作类中定义insert方法 执行最终的插入library/Db/Pdo.php
class Db_Pdo {
protected static $instance = null;
public static $TIMESTAMP_WRITES = false;
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
public function loadConfig($db) {
$this->configMaster($db['m']['host'], $db['m']['name'], $db['m']['user'], $db['m']['pwd'], $db['m']['port']);
$this->configSlave($db['s']['host'], $db['s']['name'], $db['s']['user'], $db['s']['pwd'], $db['s']['port']);
}
public function insert($table, $params = array(), $timestamp_this = null, $break = false) {
if (is_null($timestamp_this)) {
$timestamp_this = self::$TIMESTAMP_WRITES;
}
// first we build the sql query string
$columns_str = '(';
$values_str = 'VALUES (';
$add_comma = false;
// add each parameter into the query string
foreach ($params as $key => $val) {
// only add comma after the first parameter has been appended
if ($add_comma) {
$columns_str .= ', ';
$values_str .= ', ';
} else {
$add_comma = true;
}
// now append the parameter
$columns_str .= "$key";
$values_str .= ":$key";
}
// add the timestamp columns if neccessary
if ($timestamp_this === true) {
$columns_str .= ($add_comma ? ', ' : '') . 'date_created, date_modified';
$values_str .= ($add_comma ? ', ' : '') . time() . ', ' . time();
}
// close the builder strings
$columns_str .= ') ';
$values_str .= ')';
// build final insert string
$sql_str = "INSERT INTO $table $columns_str $values_str";
if ($break) {
return $sql_str;
}
// now we attempt to write this row into the database
try {
$pstmt = $this->getMaster()->prepare($sql_str);
// bind each parameter in the array
foreach ($params as $key => $val) {
$pstmt->bindValue(':' . $key, $val);
}
$pstmt->execute();
$newID = $this->getMaster()->lastInsertId();
// return the new id
return $newID;
} catch (PDOException $e) {
if (self::$SHOW_ERR == true) {
throw new Exception($e);
}
$this->pdo_exception = $e;
return false;
} catch (Exception $e) {
if (self::$SHOW_ERR == true) {
throw new Exception($e);
}
$this->pdo_exception = $e;
return false;
}
}
}