数据库快速操作类Model.class
时间:2009-02-01
来源:互联网
近期一直在学习框架,对MVC中的M比较有兴趣,所以就依照着框架里自己写了个类,呵呵,一时忘了写注释了。
附件里是一个DEMO,有兴趣的同学,可以看看,多多指教。
[php]<?php
class Model{
private $_host;
private $_user;
private $_pass;
private $_port;
private $_db;
private $_pre;
private $_charset;
public $tb;
public $fields;
public $q = NULL;
public $debug;
function Model($model){
$this->_host = CFG_DB_HOST;
$this->_user = CFG_DB_USER;
$this->_pass = CFG_DB_PASS;
$this->_port = CFG_DB_PORT;
$this->_db = CFG_DB_DB;
$this->_pre = CFG_DB_PRE;
$this->_charset = CFG_DB_CHARSET;
$this->debug = CFG_DB_DEBUG;
$this->tb = $this->_pre . $model;
$this->q = $this->connect();
mysql_select_db($this->_db);
if(!empty($this->_charset)){
$sql = "SET names " . $this->_charset;
mysql_query($sql, $this->q);
}
$this->fields = $this->getFields();
}
function connect(){
return mysql_connect($this->_host, $this->_user, $this->_pass);
}
function query($sql){
return mysql_query($sql, $this->q);
}
function execute($sql){
return mysql_query($sql, $this->q);
}
function count($field='*'){
$field = !in_array($field,$this->fields) ? '*' : $field;
$sql = "SELECT COUNT({$field}) FROM `{$this->tb}`";
$rs = $this->query($sql);
$row = mysql_fetch_row($rs);
return $row[0];
}
function numRows(){
$sql = "SELECT * FROM `{$this->tb}`";
$rs = $this->query($sql);
return mysql_num_rows($rs);
}
function toArray($rs){
if(empty($rs)) return NULL;
$rows = array();
while($row = mysql_fetch_array($rs)){
$rows[] = $row;
}
return $rows;
}
function fetch($rs, $resultType='array'){
$rows = $this->toArray($rs);
return $resultType=='object' ? (object)$rows : $rows;
}
function find($conditions='', $fields=array(), $orders='', $limit=array(),$resultType='array'){
$fieldString = '';
if(empty($fields) || !is_array($fields)){
$fieldString = '*';
}
else{
$i = 0;
foreach($fields as $field){
if(in_array($field,$this->fields))
$fieldString .= ($i++>0 ? ',' : '') . "`{$field}`";
}
unset($i);
}
$conditions = empty($conditions) ? '' : "WHERE {$conditions} ";
$order = empty($order) ? '' : "ORDER BY {$order} ";
$orderString = '';
if(empty($orders) || !is_array($orders)){
$orderString = '';
}
else{
$i = 0;
foreach($orders as $orderKey=>$orderValue){
if(in_array($orderKey,$this->fields)){
$orderString .= ($i++>0 ? ',' : '') . "`{$orderKey}` " . strtoupper($orderValue);
}
}
unset($i);
$orderString = ' ORDER BY ' .$orderString;
}
if(empty($limit) || !is_array($limit)){
$limit = '';
}
else if(1 == count($limit)){
$limit = " LIMIT " . (int)$limit[0];
}
else if(1 < count($limit)){
$limit = " LIMIT " . (int)$limit[0] . ',' . (int)$limit[1];
}
$sql = "SELECT " . $fieldString . " FROM `{$this->tb}` " . $conditions . $orderString . $limit;
$rs = $this->query($sql);
return $this->fetch($rs, $resultType);
}
function getAll($conditions='', $fields=array(), $orders=array(), $limit=array(), $resultType='array'){
return $this->find($conditions, $fields, $orders, $limit, $resultType);
}
function getOne($conditions='', $fields=array(), $orders=array(), $resultType='array'){
$rs = $this->find($conditions, $fields, $orders, array(0,1));
$row = $rs[0];
return $resultType=='object' ? (object)$row : $row;
}
function parser($options=array()){
if(!is_array($options) || empty($options)) return '';
$string = '';
$i = 0;
foreach($options as $key => $value){
if(in_array($key,$this->fields))
$string .= ($i++>0?',':'') . "`{$key}`='" .$value."'";
}
return $string;
}
function save($data=array()){
if(empty($data) || !is_array($data)) die('Model::save($data) $data is NULL or not a array');
echo $sql = "INSERT INTO `{$this->tb}` SET " . $this->parser($data);
$this->query($sql);
return $this->insertId() ? true : false;
}
function add($data=array()){
if(empty($data) || !is_array($data)) die('Model::add($data) $data is NULL or not a array');
return $this->save($data);
}
function insert($data=array()){
if(empty($data) || !is_array($data)) die('Model::insert($data) $data is NULL or not a array');
return $this->save($data);
}
function delete($conditions=''){
$sql = "DELETE FROM `{$this->tb}` " . (empty($conditions) ? '' : " WHERE {$conditions}");
$this->query($sql);
return empty($error) ? true : false;
}
function update($data=array(), $conditions=''){
if(empty($data) || !is_array($data)) die('Model::update($data) $data is NULL or not a array');
$sql = "UPDATE `{$this->tb}` SET " . $this->parser($data) . (empty($conditions) ? '' : " WHERE {$conditions}");
$this->query($sql);
$error = mysql_errno();
return empty($error) ? true : false;
}
function insertId(){
return mysql_insert_id($this->q);
}
function affectedRows(){
return mysql_affected_rows($this->q);
}
function getFields(){
$fieldArray = mysql_list_fields($this->_db, $this->tb, $this->q);
$fieldLen = mysql_num_fields($fieldArray);
$fields = array();
for($i=0; $i < $fieldLen; $i++){
$fields[] = mysql_field_name($fieldArray, $i);
}
return $fields;
}
function close(){
mysql_close($this->q);
return true;
}
}
?>[/php]
例子:
<?php
include_once 'Model.class.php';
class topicModel extends Model{
}
?>
<?php
/**
* @demo for Model
*/
include 'appModel.class.php';
$model = D('topic');
//count records total
//echo $model->count();
//$rs = $model->find('`help_topic_id`=1',array('help_topic_id','name','url','example'),array('help_topic_id'=>'DESC','url'=>'ASC'),array('0','10'));
//$rs = $model->getOne('`help_topic_id`=2');
//var_dump($rs);
//print_r($model->getFields());
$insert = array(help_topic_id => '497',
name => 'deeka',
help_category_id => '1',
description => 'deeka.cn',
example => 'http://deeka.cn',
url => 'http://deeka.cn'
);
//var_dump($model->add($insert));
//var_dump($model->update(array('help_category_id'=>20),'help_topic_id=497'));
//var_dump($model->delete('help_topic_id=497'));
?>
附件里是一个DEMO,有兴趣的同学,可以看看,多多指教。
[php]<?php
class Model{
private $_host;
private $_user;
private $_pass;
private $_port;
private $_db;
private $_pre;
private $_charset;
public $tb;
public $fields;
public $q = NULL;
public $debug;
function Model($model){
$this->_host = CFG_DB_HOST;
$this->_user = CFG_DB_USER;
$this->_pass = CFG_DB_PASS;
$this->_port = CFG_DB_PORT;
$this->_db = CFG_DB_DB;
$this->_pre = CFG_DB_PRE;
$this->_charset = CFG_DB_CHARSET;
$this->debug = CFG_DB_DEBUG;
$this->tb = $this->_pre . $model;
$this->q = $this->connect();
mysql_select_db($this->_db);
if(!empty($this->_charset)){
$sql = "SET names " . $this->_charset;
mysql_query($sql, $this->q);
}
$this->fields = $this->getFields();
}
function connect(){
return mysql_connect($this->_host, $this->_user, $this->_pass);
}
function query($sql){
return mysql_query($sql, $this->q);
}
function execute($sql){
return mysql_query($sql, $this->q);
}
function count($field='*'){
$field = !in_array($field,$this->fields) ? '*' : $field;
$sql = "SELECT COUNT({$field}) FROM `{$this->tb}`";
$rs = $this->query($sql);
$row = mysql_fetch_row($rs);
return $row[0];
}
function numRows(){
$sql = "SELECT * FROM `{$this->tb}`";
$rs = $this->query($sql);
return mysql_num_rows($rs);
}
function toArray($rs){
if(empty($rs)) return NULL;
$rows = array();
while($row = mysql_fetch_array($rs)){
$rows[] = $row;
}
return $rows;
}
function fetch($rs, $resultType='array'){
$rows = $this->toArray($rs);
return $resultType=='object' ? (object)$rows : $rows;
}
function find($conditions='', $fields=array(), $orders='', $limit=array(),$resultType='array'){
$fieldString = '';
if(empty($fields) || !is_array($fields)){
$fieldString = '*';
}
else{
$i = 0;
foreach($fields as $field){
if(in_array($field,$this->fields))
$fieldString .= ($i++>0 ? ',' : '') . "`{$field}`";
}
unset($i);
}
$conditions = empty($conditions) ? '' : "WHERE {$conditions} ";
$order = empty($order) ? '' : "ORDER BY {$order} ";
$orderString = '';
if(empty($orders) || !is_array($orders)){
$orderString = '';
}
else{
$i = 0;
foreach($orders as $orderKey=>$orderValue){
if(in_array($orderKey,$this->fields)){
$orderString .= ($i++>0 ? ',' : '') . "`{$orderKey}` " . strtoupper($orderValue);
}
}
unset($i);
$orderString = ' ORDER BY ' .$orderString;
}
if(empty($limit) || !is_array($limit)){
$limit = '';
}
else if(1 == count($limit)){
$limit = " LIMIT " . (int)$limit[0];
}
else if(1 < count($limit)){
$limit = " LIMIT " . (int)$limit[0] . ',' . (int)$limit[1];
}
$sql = "SELECT " . $fieldString . " FROM `{$this->tb}` " . $conditions . $orderString . $limit;
$rs = $this->query($sql);
return $this->fetch($rs, $resultType);
}
function getAll($conditions='', $fields=array(), $orders=array(), $limit=array(), $resultType='array'){
return $this->find($conditions, $fields, $orders, $limit, $resultType);
}
function getOne($conditions='', $fields=array(), $orders=array(), $resultType='array'){
$rs = $this->find($conditions, $fields, $orders, array(0,1));
$row = $rs[0];
return $resultType=='object' ? (object)$row : $row;
}
function parser($options=array()){
if(!is_array($options) || empty($options)) return '';
$string = '';
$i = 0;
foreach($options as $key => $value){
if(in_array($key,$this->fields))
$string .= ($i++>0?',':'') . "`{$key}`='" .$value."'";
}
return $string;
}
function save($data=array()){
if(empty($data) || !is_array($data)) die('Model::save($data) $data is NULL or not a array');
echo $sql = "INSERT INTO `{$this->tb}` SET " . $this->parser($data);
$this->query($sql);
return $this->insertId() ? true : false;
}
function add($data=array()){
if(empty($data) || !is_array($data)) die('Model::add($data) $data is NULL or not a array');
return $this->save($data);
}
function insert($data=array()){
if(empty($data) || !is_array($data)) die('Model::insert($data) $data is NULL or not a array');
return $this->save($data);
}
function delete($conditions=''){
$sql = "DELETE FROM `{$this->tb}` " . (empty($conditions) ? '' : " WHERE {$conditions}");
$this->query($sql);
return empty($error) ? true : false;
}
function update($data=array(), $conditions=''){
if(empty($data) || !is_array($data)) die('Model::update($data) $data is NULL or not a array');
$sql = "UPDATE `{$this->tb}` SET " . $this->parser($data) . (empty($conditions) ? '' : " WHERE {$conditions}");
$this->query($sql);
$error = mysql_errno();
return empty($error) ? true : false;
}
function insertId(){
return mysql_insert_id($this->q);
}
function affectedRows(){
return mysql_affected_rows($this->q);
}
function getFields(){
$fieldArray = mysql_list_fields($this->_db, $this->tb, $this->q);
$fieldLen = mysql_num_fields($fieldArray);
$fields = array();
for($i=0; $i < $fieldLen; $i++){
$fields[] = mysql_field_name($fieldArray, $i);
}
return $fields;
}
function close(){
mysql_close($this->q);
return true;
}
}
?>[/php]
例子:
<?php
include_once 'Model.class.php';
class topicModel extends Model{
}
?>
<?php
/**
* @demo for Model
*/
include 'appModel.class.php';
$model = D('topic');
//count records total
//echo $model->count();
//$rs = $model->find('`help_topic_id`=1',array('help_topic_id','name','url','example'),array('help_topic_id'=>'DESC','url'=>'ASC'),array('0','10'));
//$rs = $model->getOne('`help_topic_id`=2');
//var_dump($rs);
//print_r($model->getFields());
$insert = array(help_topic_id => '497',
name => 'deeka',
help_category_id => '1',
description => 'deeka.cn',
example => 'http://deeka.cn',
url => 'http://deeka.cn'
);
//var_dump($model->add($insert));
//var_dump($model->update(array('help_category_id'=>20),'help_topic_id=497'));
//var_dump($model->delete('help_topic_id=497'));
?>

model.class.rar (2.61 KB)
作者: deeka 发布时间: 2009-02-01
支持,学习了

作者: hiptc 发布时间: 2009-02-01
ding,下来看看.

作者: 渔洋童话 发布时间: 2009-02-03
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28