yaf本身不带orm数据封装,不过可以建立在pdo上实现自己的封装。
一:使用php单例模式,建立基类。libraryBase.php
二:以注册用户信息为例,$userInfo为表单传入的数据,我们要插入到数据库
我们建立了userModel类class userModel extends Model { } $uid = userModel::getInstance()->add($userInfo);
我们用add方法插入数据,我们需要建立这个方法在Model中
三:ORM 集成到libraryModel.php字段初始值 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.phpconfigMaster($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; } } }