删除Think/Model/Lite 类

增加db函数用于实例化Db类
This commit is contained in:
ThinkPHP
2013-04-28 10:19:29 +08:00
parent 9790100ad6
commit 76bcf6259b
2 changed files with 10 additions and 240 deletions

View File

@@ -1,240 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Model;
class Lite {
// 当前数据库操作对象
protected $db = null;
// 数据库名称
protected $dbName = '';
//数据库配置
protected $connection = '';
// 数据表前缀
protected $tablePrefix = '';
// 数据表名(不包含表前缀)
protected $tableName = '';
// 实际数据表名(包含表前缀)
protected $trueTableName = '';
// 最近错误信息
protected $error = '';
// 配置参数
protected $config = [];
/**
* 架构函数
* 取得DB类的实例对象 字段检查
* @access public
* @param string $name 模型名称
* @param array $config 模型配置
*/
public function __construct($name='',$config=[]) {
// 传入模型参数
if(!empty($name)){
$this->name = $name;
}elseif(empty($this->name)){
$this->name = $this->getModelName();
}
if(strpos($this->name,'.')) { // 支持 数据库名.模型名的 定义
list($this->dbName,$this->name) = explode('.',$this->name);
}
if(isset($config['table_prefix'])) {
$this->tablePrefix = $config['table_prefix'];
}
if(isset($config['connection'])) {
$this->connection = $config['connection'];
}
if(isset($config['table_name'])) {
$this->tableName = $config['table_name'];
}
if(isset($config['true_table_name'])) {
$this->trueTableName = $config['true_table_name'];
}
if(isset($config['db_name'])) {
$this->dbName = $config['db_name'];
}
// 设置表前缀
if(empty($this->tablePrefix)) {
$this->tablePrefix = is_null($this->tablePrefix)?'':C('database.prefix');
}
// 数据库初始化操作
// 获取数据库操作对象
// 当前模型有独立的数据库连接信息
$this->db(0,$this->connection);
}
/**
* 得到当前的数据对象名称
* @access public
* @return string
*/
public function getModelName() {
if(empty($this->name))
$this->name = substr(get_class($this),0,-5);
return $this->name;
}
/**
* 得到完整的数据表名
* @access public
* @return string
*/
public function getTableName() {
if(empty($this->trueTableName)) {
$tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
if(!empty($this->tableName)) {
$tableName .= $this->tableName;
}else{
$tableName .= parse_name($this->name);
}
$this->trueTableName = strtolower($tableName);
}
return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName;
}
/**
* SQL查询
* @access public
* @param string $sql SQL指令
* @param array $binding 参数绑定
* @return mixed
*/
public function query($sql,$binding=[]) {
$sql = $this->parseSql($sql);
return $this->db->query($sql,$binding);
}
/**
* 执行SQL语句
* @access public
* @param string $sql SQL指令
* @param array $binding 参数绑定
* @return false | integer
*/
public function execute($sql,$binding=[]) {
$sql = $this->parseSql($sql);
return $this->db->execute($sql,$binding);
}
/**
* 解析SQL语句
* @access public
* @param string $sql SQL指令
* @return string
*/
protected function parseSql($sql) {
// 分析表达式
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->config['DB_PREFIX']));
$this->db->setModel($this->name);
return $sql;
}
/**
* 切换当前的数据库连接
* @access public
* @param integer $linkNum 连接序号
* @param mixed $config 数据库连接信息
* @return Model
*/
public function db($linkNum='',$config=''){
if(''===$linkNum && $this->db) {
return $this->db;
}
static $_linkNum = [];
static $_db = [];
if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $config && $_linkNum[$linkNum]!=$config) ) {
// 创建一个新的实例
if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数
$config = Config::get($config);
}
$_db[$linkNum] = \Think\Db::instance($config,true);
}elseif(NULL === $config){
$_db[$linkNum]->close(); // 关闭数据库连接
unset($_db[$linkNum]);
return ;
}
// 记录连接信息
$_linkNum[$linkNum] = $config;
// 切换数据库连接
$this->db = $_db[$linkNum];
return $this;
}
/**
* 启动事务
* @access public
* @return void
*/
public function startTrans() {
$this->commit();
$this->db->startTrans();
return ;
}
/**
* 提交事务
* @access public
* @return boolean
*/
public function commit() {
return $this->db->commit();
}
/**
* 事务回滚
* @access public
* @return boolean
*/
public function rollback() {
return $this->db->rollback();
}
/**
* 返回模型的错误信息
* @access public
* @return string
*/
public function getError(){
return $this->error;
}
/**
* 返回数据库的错误信息
* @access public
* @return string
*/
public function getDbError() {
return $this->db->getError();
}
/**
* 返回最后插入的ID
* @access public
* @return string
*/
public function getLastInsID() {
return $this->db->getLastInsID();
}
/**
* 返回最后执行的sql语句
* @access public
* @return string
*/
public function getLastSql() {
return $this->db->getLastSql($this->name);
}
}

View File

@@ -92,6 +92,16 @@ function D($name='',$layer='Model') {
return Think\Loader::model($name,$layer);
}
/**
* 实例化数据库类
* @param array $config 数据库配置参数
* @param boolean $lite 是否lite连接
* @return object
*/
function db($config=[],$lite=false) {
return Think\Db::instance($config,$lite);
}
/**
* 实例化控制器 格式:[模块/]控制器
* @param string $name 资源地址