首页 | PHP资讯 | 技术专栏 | 资源共享 | PHP培训 | PHP职场 | 图书 | PHP ON WIN | PHP圈子 | PHPer学习大本营
返回列表 回复 发帖

共享一下自己的Model类,用类控制数据库操作(aspire_mode)

共享一下自己的Model类,用类控制数据库操作(aspire_mode)

学习了ZF的框架,对数据库操作这块感觉操作非常方便,利用对象代替了那繁杂的SQL语句,可得用ZF也够麻烦的,虽说它是弱藕合型的,对数据库操作的代码可以单独用,可初始化进还要在PHP.INI里设置incldueZF的路径,要么是在PHP页面开头set_incldue_path(xxoo);的操作,丫的,真麻烦。一怒之下本人自己也来写个。
经过几晚的调试,终于大功告成。
先来介绍一下用法,再来发源码
操作方法:
一,初始化:
  1. include 'Db_Mysqli.class.php';
  2. include 'Model.class.php';
复制代码
先导入两个类文件
  1. $config = array(
  2. 'host'=>'localhost',
  3. 'username'=>'root',
  4. 'password'=>'123456',
  5. 'dbname'=>'demo',
  6. 'charset'=>'gbk',
  7. );
复制代码
定义个数据库信息的数组

然后就是创建个类了,例:我对数据库demo中的LAMP数据表进行操作
  1. class LampModel extends Model {
  2.         
  3. }
复制代码
就建一个LampModel的类,如上所示。
接着就事例化一个对象:
  1. $m = new LampModel();
复制代码
这样就完成了初始化工作,
这里在类的命名上你可能要问,为什么Lamp后要加Model,直接用数据表名多好呀
主要目的就是想让人一目了然,一看命名就知道对象$m就是对数据库操作的,一般都这么用,看起来比较正规。有意义(观众:狂吐不止)
如果不想创建类,可以直接实例化一个对象,直接用Model类
  1. $m = new
  2. Model()
复制代码
然后在对象里定义数据表
  1. $m->tablename='lamp';
复制代码
注:$m = new Model();

这种用法我不推荐,因为在此类的设计上采用了singleston(单件模式)
最大限度地提升效率,节省资源。
使用方法:
  1. $m = Mode::getinstance();
复制代码
从而将
$m = new Model();

取而代之
附件: 您需要登录才可以下载或查看附件。没有帐号?注册
www.tommyframework.com--简单易用的php框架

二,查询操作
1,from()函数的用法:
from(参数1,[参数2])
  1. $d = $m->from('lamp','post_subject');
复制代码
'lamp'为要查询的数据表
'post_subject'为查询的数据表字段
注:这时支持多表查询
不过多表,多字段时操作要使用数组
  1. $tb = array('lamp', 'member');
  2. $item = array('lamp.post_id','membe.name');

  3. $d = $m->from($tb,$item);
复制代码
此上是对lamp和member两个数据表进行查询,所要查询的字段为'lamp.post_id','membe.name'

注;当参数2为空时,默认为对整体数据表字段进行查询,相当于
  1. select *
复制代码
-------------------------------------------------------
2,where函数的用法:
where(参数1);
  1. $d=$m->where('id=23');
复制代码
注:如果你的参数里含有字符串
如:name=streen003
要对streen003时行转义
怎么转义呢
要用到本类的另一个函数:quote_into()
  1. $where = $m->quote_into('name=?', 'streen003');
复制代码
这样就转义完了javascript:;
然后:
  1. $d=$m->where($where);
复制代码
注:如果参数里有多个条件,要使用数据
  1. $where_arr = array('id>23', 'age==25');
  2. $d=$m->where($where_arr);
复制代码
--------------------------------------------------
3,orwhere()函数的用法:
orwhere(参数1)
  1. $d=$m->orwhere('id=26');
复制代码
操作和函数where()一样,无须多言
  1. $where=$m->quote_into('name=?,'王经臣');
  2. $d=$m->orwhere($where);
复制代码
  1. $where_arr = array('id>23', 'age==25');
  2. $d=$m->orwhere($where_arr);
复制代码
----------------------------------------------
4,order()函数的用法:
order(参数1)
  1. $d=$m->order('id asc');
复制代码
参数里有多个条件同样要用数组
  1. $order_arr = array('id desc', 'name adc');
  2. $d=$m->order($order_arr);
复制代码
-------------------------------------
5,limit()函数的用法
limit(参数1,[参数2])
  1. $d=$m->limit(10,20);
复制代码
注:两个参数必须为int型的,参数2可为空。
www.tommyframework.com--简单易用的php框架

TOP

6,select()函数的用法:
  1. $m->from('lamp')->where('id<23')->order('id desc')->select();
复制代码
参数为空,其作用就是将前面from ,where,等函数生成的sql语句进行执行。
如果我想查一下from,where函数生成的sql语名
可:
  1. echo $m->from('lamp')->where('id<23')->order('id desc');
复制代码
三,主键查找
首先强调一点,这里所说的主键并不一定是你数据表里的主键
这个主键是相对与本操作来说的,是可以人为设置的
设置方法:
  1. $m->primary_key='name';
复制代码
这样主键就设置为的字段name上了。如果没有设置,程序默认为数据表的主键。
1,find()函数的用法
  1. $d =$m->find('23');
复制代码
查找主键为23的数据
注:这里也可以对多个数据进行查询,多数据查询时要用数据
,当参数为数组时,输出的数据为*文明用语*数组,参数为非数组时,返回的数据为两维数组
相当于mysql_fetch_assoc()获取的数据。
  1. $item = array('23', '7','13');
  2. $d=$m->find($item);
复制代码
注:这里输出数据的排序是可以设置的,默认为DESC,原因就是目前常用的排序大多是DESC的,如新闻网站,最新的新闻一般在最上面。
排序设置,操作:
  1. $m->order='asc';
复制代码
不过要用在find()函数之前才有效。这个同样对后面要讲的finAll()函数也有效
2,findAll()的用法
$d=$m->findAll();
参数为空,就是获取表内的全部数据,以主键排序输出
排序设置,前面已经提到
  1. $m->order='asc';
复制代码
要用在findAll()这前,默认为DESC的
www.tommyframework.com--简单易用的php框架

TOP

四,添加数据
insert()函数用法
insert(array(参数1))
  1. $content = array('name'=>'streen003', 'age'=>'23');

  2. $m->insert($content);
复制代码
注:添加的数据参数为数组,索引为数据表字段,value为要添加的内容。
如果数组中索引含有数据表没有的字段,程序则自动过滤掉。


五,更新数据
update()函数的用法
  1. $content = array('name'=>'streen003', 'age'=>'23');
  2. $m->update($content);
复制代码
注:参数为数组,索引为数据表字段,value为要更改的内容
如果数组中索引含有数据表没有的字段,程序则自动过滤掉。


六:获取数据对象
1,fetchRow()函数的用法
fetchRow(参数1)
  1. $d = $m->fetchRow('id=23');
复制代码
注:参数中含有字符串,要用quote_into()进行转义
  1. $param = $m->quote_into('name=?','streen003');
  2. $d = $m->fetchRow($param);
复制代码
-------------------------------------------
2,save()函数的用法
save()
注:参数为空,作用是对生成的对象数据进行保存,(如用fetchRow获取的数据进行更新,相当于update,对于createRow生成的数据则是进行保存,相当于insert)
  1. $d = $m->fetchRow('id=23');
  2. $d->name='streen003';
  3. $d->save();
复制代码
  1. $d= $m->createRow();
  2. $d->name = 'streen003';
  3. $d->age='25';
  4. $d->save();
复制代码
3,createRow()函数的用法
createtRow()
注:参数为空,作用是生成一个对象型数据,常用save()合用
  1. $d= $m->createRow();
  2. $d->name = 'streen003';
  3. $d->age='25';
  4. $d->save();
复制代码
七,SQL语句操作
为什么加这个操作呢,用类控制数据表不是不用SQL语句了吗,是呀,用类控制数据表
单表操作很爽,多表操作很烦,这是有目共睹的。当遇到非常复杂的操作时,或你的SQL写的很棒,或者前面的那些函数无法满足你的要求,可以使用这步。
execute()函数的用法
execute(参数1)
注:参数为SQL语句,
  1. $sql= 'select post_name form lamp';
  2. $d=$m->execute($sql);
复制代码
一步到位,哈哈。这样就将SQL语句后的所有数据都返回了,注:本类所有函数返回的数据索引都是字段型的,因为非字段型的数据在日常PHP开发中用处不大。

八,数据缓存操作]
程序在执行过程序中,反复对数据表安段进行查询,这样运行效率就打了折扣
所以为了解决这一问题,特加入了缓存,将第一次数据表字段查询的结果生成缓存文件
此后用到直接从缓存中读取
使用此类缓存操作,要设置的就是缓存文件的存放路径
  1. $m->cache_dir='/cache/data/';
复制代码
程序默认的缓存路径为当前Model类所在的目录下的Data文件夹,当不存大缓存目录时,程序自动创建。
-------------------------------
到这里本类的所有功能已经介绍完毕
www.tommyframework.com--简单易用的php框架

TOP

程序源码:希望高手指点一下,还有哪里有改进的
  1. <?php

  2. // +---------------------------------------------------------------
  3. // |         Aspire Mode Class
  4. // +---------------------------------------------------------------
  5. // | Copyright (c) 2009 http://www.bc263.com All rights reserved.
  6. // +---------------------------------------------------------------
  7. // | Author: streen003 <streen003gmail.com>
  8. // +---------------------------------------------------------------

  9. class Model {
  10.        
  11.         //定义变量
  12.         private    $table_info;  //数据表信息
  13.         private    $option;      //SQL语句选项
  14.         protected  $primary_key; //数据表主键
  15.         protected  $table_name;  //数据表名
  16.         protected  $table_prefix; //数据表前缀
  17.         protected  $class_name;  //本类的名称
  18.        
  19.         protected  $db;                        //Mysql Server连接
  20.         private    $params;      //config数据库信息
  21.         protected  $table_field;//数据表字段信息
  22.         protected  $myrow;      //返回数据
  23.        
  24.         protected  $order;      //数据列表的排序,用于 ORDER BY
  25.         protected  $quto_content;//函数quote_into()所用来暂存数据的
  26.         protected  static $instance; //用于构建类的singleton模式参数
  27.         protected  $cache_dir;        //缓存文件目录
  28.         protected  $cache;                //缓存开启开关
  29.        
  30.        
  31.         //构造函数,用于初始化运行环境
  32.         public function __construct() {
  33.                
  34.                 global $config; //导入数据库连接信息

  35.                 if (!$this->db) {
  36.                        
  37.                         if (!$this->params) {
  38.                                
  39.                                 $this->params = $config;
  40.                         }
  41.                        
  42.                         $this->db = Db_Mysqli::getInstance($this->params);
  43.                 }
  44.                
  45.                 $this->cache = 1;
  46.                
  47.                 return true;
  48.         }
  49.        
  50. //        +---------------------------------------------------
  51. //        |                        第一部分: 数据表信息处理
  52. //        +---------------------------------------------------

  53.         //清除变量,$string参数为$this->string对象型
  54.         protected function clear($string) {
  55.                
  56.                 if($string) {
  57.                        
  58.                         unset($string);
  59.                 }
  60.         }
  61.        
  62.        
  63.         //加载$this->table_name
  64.         protected function parse_table_name() {
  65.                
  66.                 if(!$this->table_name) {
  67.                        
  68.                         $this->get_table_name();
  69.                 }
  70.                
  71.                 return $this->table_name;
  72.         }

  73.        
  74.         //加载$this->tabl_info
  75.         protected function parse_table_info() {
  76.                
  77.                 if(!$this->table_info) {
  78.                        
  79.                         $this->get_table_info();
  80.                 }
  81.                
  82.                 return $this->table_info;
  83.         }
  84.        
  85.         //加载$this->primary_key
  86.         protected function parse_table_primarykey() {
  87.                
  88.                 if(!$this->primary_key) {
  89.                        
  90.                         $this->get_table_primarykey();
  91.                 }
  92.                
  93.                 return $this->primary_key;
  94.         }
  95.        
  96.         //加载$this->table_field
  97.         protected function parse_table_field() {
  98.                
  99.                 if(!$this->table_field) {
  100.                        
  101.                         $this->get_table_field();
  102.                 }
  103.                
  104.                 return $this->table_field;
  105.         }
  106.        
  107.         //获取类名
  108.         protected  function get_class_name() {
  109.                
  110.                 if (!$this->class_name) {
  111.                        
  112.                         $class_name = get_class($this);
  113.                         $class_name= strtolower($class_name);
  114.                        
  115.                         $this->class_name = $class_name;
  116.                 }
  117.                
  118.                 return $this->class_name;
  119.         }
  120.        
  121.         //获取数据表前缀
  122.         protected function get_table_prefix() {
  123.                
  124.                 if (!$this->table_prefix) {
  125.                        
  126.                         $this->table_prefix = (!empty($this->params['prefix'])) ? $this->params['prefix'] : '';
  127.                 }
  128.                
  129.                 return $this->table_prefix;
  130.         }
  131.        
  132.         //获取数据表信息
  133.         protected   function get_table_info() {
  134.                
  135.                 $this->parse_table_name();
  136.                
  137.                 $sql="SHOW FIELDS FROM {$this->table_name}";
  138.                
  139.                 $this->table_info = $this->db->get_array($sql);
  140.                
  141.                 return $this->table_info;
  142.         }
  143.        
  144.         //获取数据表名
  145.         protected        function get_table_name() {
  146.                
  147.                 $this->get_class_name();
  148.                
  149.                 $this->get_table_prefix();
  150.                
  151.                 $this->table_name = (!empty($this->table_prefix)) ? $this->table_prefix.substr($this->class_name,0,-5) : substr($this->class_name,0,-5);
  152.                
  153.                 return $this->table_name;
  154.         }
  155.        
  156.         //cache_file文件生成
  157.         protected function parse_cache_file($name) {
  158.                
  159.                 if(!$this->cache_dir) {
  160.                        
  161.                         $this->cache_dir = './Data/';
  162.                 }
  163.                
  164.                 $this->parse_table_name();
  165.                
  166.                 $cache_file = $this->cache_dir.$this->table_name.'_'.$name.'.data.php';
  167.                
  168.                 return $cache_file;
  169.         }
  170.        
  171.         //生成缓存文件
  172.         protected function create_cache($name, $data) {
  173.                
  174.                 $cache_file = $this->parse_cache_file($name);
  175.                
  176.                 $content = '<?php ';
  177.                 $content .= '

  178. .$name.'_cache = ';
  179.                 $content .= var_export($data,true).';';
  180.                 $content .= ' ?>';
  181.                
  182.                 //判断cache_dir是否存在,不存在则建立目录
  183.                 if (!is_dir($this->cache_dir)) {
  184.                        
  185.                         mkdir($this->cache_dir,'0777');
  186.                 }
  187.                
  188.                 file_put_contents($cache_file,$content,LOCK_EX);
  189.                
  190.                 return true;
  191.         }
  192.        
  193.         //加载缓存文件
  194.         protected function load_cache($name) {
  195.                
  196.                 $cache_file = $this->parse_cache_file($name);
  197.                
  198.                 include($cache_file);
  199.                
  200.                 return ${$name.'_cache'};
  201.                
  202.         }
  203.        
  204.         //获取数据表的主键
  205.         protected  function get_table_primarykey() {
  206.                
  207.                 if ( $this->cache && file_exists( $this->parse_cache_file('primarykey') ) ) {
  208.                        
  209.                         $this->primary_key = $this->load_cache('primarykey');
  210.                 }
  211.                 else {
  212.                        
  213.                         $this->parse_table_info();
  214.                
  215.                         foreach ($this->table_info as $val) {
  216.                        
  217.                                 if ($val['Key']=='PRI') {
  218.                                        
  219.                                         $this->primary_key = $val['Field'];
  220.                                 }
  221.                         }
  222.                        
  223.                         $this->create_cache('primarykey',$this->primary_key);
  224.                 }
  225.                
  226.                 return $this->primary_key;
  227.         }
  228.        
  229.         //获取数据表字段信息
  230.         public  function get_table_field() {
  231.                
  232.                 if ( $this->cache && file_exists( $this->parse_cache_file('field') ) ) {
  233.                        
  234.                         $this->table_field = $this->load_cache('field');
  235.                 }
  236.                 else {
  237.                        
  238.                         $this->parse_table_info();
  239.                        
  240.                         $fields = array();
  241.                        
  242.                         foreach ($this->table_info as $val) {
  243.                                
  244.                                 $fields[] = $val['Field'];
  245.                         }
  246.                        
  247.                         $this->table_field = $fields;
  248.                        
  249.                         $this->create_cache('field',$this->table_field);
  250.                 }
  251.                
  252.                 return $this->table_field;
  253.         }

  254.        
  255. //        +---------------------------------------------------
  256. //        |                        第二部分: Select SQL 语句处理
  257. //        +---------------------------------------------------
  258.        
  259.        
  260.         //from('数据表','查询字段')用于处理 SELECT fields FROM table之类的SQL语句部分
  261.         public function from($name, $item=false) {
  262.                
  263.                 if (!empty($name)) {
  264.                        
  265.                         $table_str = $this->parse_options($name);
  266.                        
  267.                         $item_str = ($item==true) ? $this->parse_options($item) : '*';
  268.                        
  269.                         $this->clear($this->option['from']);
  270.                        
  271.                         $this->option['from'] = 'SELECT '.$item_str.' FROM '.$table_str;
  272.                        
  273.                         return $this;
  274.                 }
  275.                 else {
  276.                         return false;
  277.                 }
  278.         }
  279.        
  280.        
  281.         //where('查询条件')用于处理 WHERE id=0531 诸如此类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
  282.         public function where($string) {
  283.                
  284.                 if (!empty($string)) {
  285.                        
  286.                         $where_str = $this->parse_options($string,true);
  287.                        
  288.                         $this->option['where'] .= ($this->option['where']) ? ' AND '.$where_str : ' WHERE '.$where_str;
  289.                        
  290.                         return $this;
  291.                 }
  292.                 else {
  293.                         return false;
  294.                 }
  295.         }
  296.        
  297.        
  298.         //or_where('查询条件')用于处理 OR WHERE id=0531 诸如此类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
  299.         public function orwhere($string) {
  300.                
  301.                 if (!empty($string)) {
  302.                        
  303.                         $or_where_str = $this->parse_options($string,true);
  304.                        
  305.                         $this->option['or_where'] .= ($this->option['or_where']) ? ' AND '.$or_where_str : ' OR '.$or_where_str;
  306.                        
  307.                         return $this;
  308.                 }
  309.                 else {
  310.                         return false;
  311.                 }
  312.         }
  313.        
  314.        
  315.         //order('排列条件')用于处理 ORDER BY post_id ASC 之类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
  316.         public function order($string) {
  317.                
  318.                 if (!empty($string)) {
  319.                        
  320.                         $order_str = $this->parse_options($string);
  321.                        
  322.                         $this->option['order'] .= ($this->option['order']) ? ' AND '.$order_str : ' ORDER BY '.$order_str;
  323.                        
  324.                         return $this;
  325.                 }
  326.                 else {
  327.                         return false;
  328.                 }
  329.         }
  330.        
  331.        
  332.         //limit(10,20)用于处理LIMIT 10, 20之类的SQL语句部分
  333.         public function limit($num1, $num2) {
  334.                
  335.                 if(is_int($num1)) {
  336.                        
  337.                         $num1 = trim($num1);
  338.                        
  339.                         $num2 = (is_int($num2)) ? trim($num2) : '';
  340.                        
  341.                         $limit_str = $num2 ? $num1.', '.$num2 : $num1;
  342.                        
  343.                         $this->clear($this->option['limit']);
  344.                        
  345.                         $this->option['limit'] = ' LIMIT '.$limit_str;
  346.                        
  347.                         return $this;
  348.                 }
  349.                 else {
  350.                        
  351.                         return false;
  352.                 }
  353.         }
  354.        
  355.        
  356.         //处理from(),where(),order()等函数参数,特别是对参数为数组的处理
  357.         protected   function parse_options($string, $info=false) {
  358.                
  359.                 if (is_array($string)) {
  360.                        
  361.                         $option_str = '';
  362.                         if ($info==true) {
  363.                                
  364.                                 foreach ($string as $val) {
  365.                                        
  366.                                         $option_str .= ' '.trim($val).' AND';
  367.                                 }
  368.                                
  369.                                 $option_str = substr($option_str,0,-3);
  370.                         }
  371.                         else {
  372.                                
  373.                                 foreach ($string as $val) {
  374.                                        
  375.                                         $option_str .= ' '.trim($val).',';
  376.                                 }
  377.                                
  378.                                 $option_str = substr($option_str,0,-1);
  379.                         }
  380.                 }
  381.                 else {
  382.                        
  383.                         $option_str = trim($string);
  384.                 }
  385.                
  386.                 return $option_str;
  387.         }
  388.        
  389.        
  390.         //引用参数,用来完成对某个参数加上单引号,方便SQL语句执行,用法:$this->quote_into('id=?', '5');
  391.         public  function quote_into($string, $param) {
  392.                
  393.                 if (!empty($string) && !empty($param)) {
  394.                        
  395.                         $string = str_replace('?', '\''.trim($param).'\'', $string);
  396.                        
  397.                         $this->clear($this->quto_content);
  398.                        
  399.                         $this->quto_content = $string;
  400.                        
  401.                         return $this->quto_content;
  402.                 }
  403.                 else {
  404.                        
  405.                         return false;
  406.                 }
  407.         }
  408.        
  409.        
  410.         //组装SQL语句并完成查询,并返回查询结果,用法$this->select();
  411.         public function select() {
  412.                
  413.                 if ($this->option['from']) {
  414.                        
  415.                         $sql=$this->option['from'];
  416.                        
  417.                         if ($this->option['where']) {
  418.                                
  419.                                 $sql .= $this->option['where'];
  420.                                
  421.                                 if ($this->option['or_where']) {
  422.                                        
  423.                                         $sql .= $this->option['or_where'];
  424.                                         unset($this->option['or_where']);
  425.                                 }
  426.                                
  427.                                 unset($this->option['where']);
  428.                         }
  429.                        
  430.                         if ($this->option['order']) {
  431.                                
  432.                                 $sql .=$this->option['order'];
  433.                                 unset($this->option['order']);
  434.                         }
  435.                        
  436.                         if ($this->option['limit']) {
  437.                                
  438.                                 $sql .= $this->option['limit'];
  439.                         }
  440.                        
  441.                         $this->clear($this->myrow);
  442.                        
  443.                         $this->myrow = $this->db->get_array($sql);
  444.                        
  445.                         return $this->myrow;
  446.                 }
  447.                 else {
  448.                        
  449.                         return false;
  450.                 }
  451.                
  452.         }
  453.        
  454.        
  455. //        +---------------------------------------------------
  456. //        |                        第三部分: Insert, Update, Delete, Find
  457. //        +---------------------------------------------------

  458.        
  459.         //根据主键,获取某个主键的一行信息,主键可以类内设置
  460.         public function find($id) {
  461.                
  462.                 if (!empty($id)) {
  463.                        
  464.                         $this->parse_table_primarykey();
  465.                        
  466.                         $this->parse_table_name();
  467.                        
  468.                         if (!$this->order) {
  469.                                
  470.                                 $this->order = 'DESC';
  471.                         }
  472.                         else {
  473.                                
  474.                                 $this->order = (in_array(strtoupper($this->order), array('ASC','DESC'))) ? strtoupper($this->order) : 'DESC';
  475.                         }
  476.                        
  477.                         $sql = 'SELECT * FROM '.$this->table_name.' WHERE '.$this->primary_key;
  478.                        
  479.                         if (is_array($id)) {
  480.                                
  481.                                 $values = $this->parse_options($id);
  482.                                 $sql .= ' IN ('.$values.')';
  483.                         }
  484.                         else {
  485.                                
  486.                                 $sql .= ' = '.trim($id);
  487.                         }
  488.                        
  489.                         $sql .= ' ORDER BY '.$this->primary_key.' '.$this->order;
  490.                        
  491.                         $this->clear($this->myrow);
  492.                        
  493.                         $this->myrow = is_array($id) ? $this->db->get_array($sql) : $this->db->fetch_row($sql);
  494.                        
  495.                         return $this->myrow;
  496.                 }
  497.                 else {
  498.                         return false;
  499.                 }
  500.         }
  501.        
  502.        
  503.         //根据主键信息,获取数据表全部信息
  504.         public function findAll() {
  505.                
  506.                 $this->parse_table_primarykey();
  507.                
  508.                 $this->parse_table_name();
  509.                
  510.                 if (!$this->order) {
  511.                        
  512.                         $this->order = 'DESC';
  513.                 }else {
  514.                        
  515.                         $this->order = (in_array(strtoupper($this->order), array('ASC','DESC'))) ? strtoupper($this->order) : 'DESC';
  516.                 }
  517.                
  518.                 $sql = 'SELECT * FROM '.$this->table_name.' ORDER BY '.$this->primary_key.' '.$this->order;
  519.                
  520.                 $this->clear($this->myrow);
  521.                
  522.                 $this->myrow = $this->db->get_array($sql);
  523.                
  524.                 return $this->myrow;
  525.         }
  526.        
  527.        
  528.         //根据某一条件,获取一行信息(字段型),注:只是一行信息
  529.         public function fetchRow($where) {
  530.                
  531.                 if (!empty($where)) {
  532.                        
  533.                         $this->parse_table_name();
  534.                        
  535.                         $sql = 'SELECT * FROM '.$this->table_name;
  536.                        
  537.                         //处理where SQL语句
  538.                         $this->clear($this->option['where']);
  539.                        
  540.                         $this->where($where);
  541.                        
  542.                         $sql .= $this->option['where'];
  543.                        
  544.                         unset($this->option['where']);
  545.                        
  546.                         $this->clear($this->myrow);
  547.                        
  548.                         $this->myrow = $this->db->fetch_row($sql);
  549.                        
  550.                         $this->myrow = (Object)$this->myrow;
  551.                        
  552.                         //return $this->myrow;
  553.                         return $this;
  554.                 }
  555.                 else {
  556.                        
  557.                         return false;
  558.                 }
  559.         }
  560.        
  561.        
  562.         //新建一行数据,对象型的
  563.         public function createRow() {
  564.                
  565.                 $this->clear($this->myrow);
  566.                
  567.                 $this->myrow = (object)$this->myrow;
  568.                
  569.                 return $this;
  570.         }
  571.        
  572.        
  573.         //向数据表写入一行信息
  574.         public function insert($content) {
  575.                
  576.                 if (is_array($content)) {
  577.                        
  578.                         $this->parse_table_name();
  579.                        
  580.                         $this->parse_table_field();
  581.                        
  582.                         $field_str = '';
  583.                         $content_str = '';
  584.                        
  585.                         $key_arr = array_keys($content);
  586.                        
  587.                         //处理所要写入内容的数组的 values 与数据表字段对应顺序
  588.                         foreach ($this->table_field as $val) {
  589.                                
  590.                                 $field_str .= ' '.$val.',';
  591.                                
  592.                                 if (in_array($val, $key_arr)) {
  593.                                        
  594.                                         $content_str .= ' \''.$content[$val].'\',';
  595.                                 }
  596.                                 else {
  597.                                        
  598.                                         $content_str .=' NULL,';
  599.                                 }
  600.                         }
  601.                        
  602.                         $field_str = substr($field_str, 0, -1);
  603.                         $content_str = substr($content_str, 0, -1);
  604.                        
  605.                         $sql = 'INSERT INTO '.$this->table_name.'('.$field_str.' )'.' VALUES ('.$content_str.')';
  606.                        
  607.                         $this->db->query($sql);
  608.                        
  609.                         return true;
  610.                 }
  611.                 else {
  612.                         return false;
  613.                 }
  614.         }
  615.        
  616.        
  617.         //删除符合一定条件的行数据,注:如果$where中含有字符串,应用$this->qutote_into()进行转义
  618.         public function delete($where) {
  619.                
  620.                 $this->parse_table_name();
  621.                
  622.                 $this->clear($this->option['where']);
  623.                
  624.                 $this->where($where);
  625.                
  626.                 $sql = 'DELETE FROM '.$this->table_name.$this->option['where'];
  627.                
  628.                 $this->db->query($sql);
  629.                
  630.                 return true;
  631.         }
  632.        
  633.        
  634.         //更新一行信息
  635.         public function update($content,$where) {
  636.                
  637.                 if (is_array($content)) {
  638.                        
  639.                         $this->parse_table_name();
  640.                        
  641.                         $this->parse_table_field();
  642.                        
  643.                         $content_str = '';
  644.                         foreach ($content as $key=>$val) {
  645.                                
  646.                                 if (in_array($key, $this->table_field)) {
  647.                                        
  648.                                         $content_str .= $key.' = \''.$val.'\',';
  649.                                 }
  650.                         }
  651.                        
  652.                         $content_str = substr($content_str, 0, -1);
  653.                        
  654.                         $sql = 'UPDATE '.$this->table_name.' SET '.$content_str;
  655.                        
  656.                         $this->clear($this->option['where']);
  657.                                
  658.                         $this->where($where);
  659.                        
  660.                         $sql .= $this->option['where'];
  661.                        
  662.                         unset($this->option['where']);
  663.                        
  664.                         $this->db->query($sql);
  665.                        
  666.                         return true;
  667.                 }
  668.                 else {
  669.                        
  670.                         return false;
  671.                 }
  672.                
  673.         }
  674.        
  675.        
  676.         //保存数据,实质函数为:update,没有ID时则insett,注:对数为对象型
  677.         public function save() {
  678.                
  679.                 if (is_object($this->myrow)) {
  680.                        
  681.                         $myrow = (array)$this->myrow;
  682.                        
  683.                         unset($this->myrow);
  684.                        
  685.                         $key_arr = array_keys($myrow);
  686.                        
  687.                         $this->parse_table_primarykey();
  688.                        
  689.                         if(in_array($this->primary_key, $key_arr)) {
  690.                                
  691.                                 $where = $this->quote_into($this->primary_key.'=?',$myrow[$this->primary_key]);
  692.                                
  693.                                 unset($myrow[$this->primary_key]);
  694.                                
  695.                                 $this->update($myrow,$where);
  696.                         }
  697.                         else {
  698.                                
  699.                                 $this->insert($myrow);
  700.                         }
  701.                        
  702.                         return true;
  703.                 }
  704.                 else {
  705.                        
  706.                         return false;
  707.                 }
  708.                
  709.         }
  710.        
  711.        
  712. //        +---------------------------------------------------
  713. //        |                        第四部分: __SET(), __DESTRUCT(), __CALL()
  714. //        +---------------------------------------------------

  715.         //根据一个SQL语句获取执行后的全部数据库(字段型)
  716.         public function execute ($sql) {
  717.                
  718.                 if (!empty($sql)) {
  719.                        
  720.                         $this->clear($this->myrow);
  721.                        
  722.                         $this->myrow = $this->db->get_array($sql);
  723.                        
  724.                         return $this->myrow;
  725.                 }
  726.                 else {
  727.                        
  728.                         return false;
  729.                 }
  730.         }

  731.         //对类内受保护对象进行赋值
  732.         public function __set($key, $val) {
  733.                
  734.                 if (is_object($this->myrow)) {
  735.                        
  736.                         return $this->myrow->$key = $val;
  737.                 }
  738.                 else {
  739.                        
  740.                         if(in_array($key, array('cache','order','table_name','primary_key','cache_dir'))) {
  741.                                
  742.                                 return $this->$key = $val;
  743.                         }
  744.                         else {
  745.                                
  746.                                 return false;
  747.                         }
  748.                 }
  749.         }
  750.        
  751.         //获得类内一个受保护数据
  752.         public function __get($val) {
  753.                
  754.                 if ($val) {
  755.                        
  756.                         return $this->$val;
  757.                 }
  758.                 else {
  759.                        
  760.                         return false;
  761.                 }
  762.                
  763.         }
  764.        
  765.         //析构函数,用于类内程序运行结束后,打扫战场
  766.         public function __destruct() {
  767.                
  768.                 $this->clear($this->params);
  769.                
  770.                 $this->clear($this->option['where']);
  771.                
  772.                 $this->clear($this->option['or_where']);
  773.                
  774.                 $this->clear($this->option['order']);
  775.                
  776.                 $this->clear($this->myrow);
  777.                
  778.         }
  779.        
  780.         //处理当类外调用不存在的方法
  781.         public function __call($method,array $args) {
  782.                
  783.                 echo 'Class Mode method '.$method.' is not exists!<br>The args is:<br>';
  784.                 foreach ($args as $val) {
  785.                         echo $val.'<br>';
  786.                 }
  787.         }
  788.        
  789.         //用于构建本类的singleton设计模式
  790.         public static function getinstance() {
  791.                
  792.                 if (self::$instance == null) {

  793.                         self::$instance = new Model();
  794.                 }
  795.                
  796.                 return self::$instance;
  797.         }
  798.        
  799.         //直接调用函数,输出内容
  800.         public function __toString() {
  801.                
  802.                 if($this->option) {
  803.                        
  804.                         $sql = $this->option['from'].$this->option['where'].$this->option['or_where'].$this->option['order'].$this->option['limit'];
  805.                        
  806.                         return (string)$sql;
  807.                 }
  808.                 else {
  809.                        
  810.                         return (string)'This is Model Class!';
  811.                 }
  812.         }
  813.        
  814. }

  815. ?>
复制代码
www.tommyframework.com--简单易用的php框架

TOP

哈哈,不错的
[url=http://bbs.e860.cn/]澄海论坛[/url]

TOP

缓存没见到有过期时间的选项   或者数据变化更新的代码

   缓存一次  再也不更新?

TOP

缓存没见到有过期时间的选项   或者数据变化更新的代码

   缓存一次  再也不更新?
ccsk 发表于 2009-7-19 11:13

这个手动更新,不是追求效率高吗?
www.tommyframework.com--简单易用的php框架

TOP

新版本又加了好多新的功能,更加灵活
www.tommyframework.com--简单易用的php框架

TOP

看了就要回帖!!!

TOP

返回列表