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

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

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

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

然后就是创建个类了,例:我对数据库demo中的LAMP数据表进行操作[code]class LampModel extends Model {
        
}[/code]就建一个LampModel的类,如上所示。
接着就事例化一个对象:[code]$m = new LampModel();[/code]这样就完成了初始化工作,
这里在类的命名上你可能要问,为什么Lamp后要加Model,直接用数据表名多好呀
主要目的就是想让人一目了然,一看命名就知道对象$m就是对数据库操作的,一般都这么用,看起来比较正规。有意义(观众:狂吐不止)
如果不想创建类,可以直接实例化一个对象,直接用Model类[code]$m = new
Model()[/code]然后在对象里定义数据表[code]$m->tablename='lamp';[/code]
注:$m = new Model();
这种用法我不推荐,因为在此类的设计上采用了singleston(单件模式)
最大限度地提升效率,节省资源。
使用方法:[code]$m = Mode::getinstance();[/code]从而将
$m = new Model();
取而代之
附件: 您需要登录才可以下载或查看附件。没有帐号?注册
二,查询操作
1,from()函数的用法:
from(参数1,[参数2])[code]$d = $m->from('lamp','post_subject');[/code]'lamp'为要查询的数据表
'post_subject'为查询的数据表字段
注:这时支持多表查询
不过多表,多字段时操作要使用数组
[code]$tb = array('lamp', 'member');
$item = array('lamp.post_id','membe.name');

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

注;当参数2为空时,默认为对整体数据表字段进行查询,相当于[code]select *[/code]-------------------------------------------------------
2,where函数的用法:
where(参数1);[code]$d=$m->where('id=23');[/code]注:如果你的参数里含有字符串
如:name=streen003
要对streen003时行转义
怎么转义呢
要用到本类的另一个函数:quote_into()[code]$where = $m->quote_into('name=?', 'streen003');[/code]这样就转义完了javascript:;
然后:[code]$d=$m->where($where);[/code]注:如果参数里有多个条件,要使用数据[code]$where_arr = array('id>23', 'age==25');
$d=$m->where($where_arr);[/code]--------------------------------------------------
3,orwhere()函数的用法:
orwhere(参数1)[code]$d=$m->orwhere('id=26');[/code]操作和函数where()一样,无须多言[code]$where=$m->quote_into('name=?,'王经臣');
$d=$m->orwhere($where);[/code][code]$where_arr = array('id>23', 'age==25');
$d=$m->orwhere($where_arr);[/code]----------------------------------------------
4,order()函数的用法:
order(参数1)[code]$d=$m->order('id asc');[/code]参数里有多个条件同样要用数组[code]$order_arr = array('id desc', 'name adc');
$d=$m->order($order_arr);[/code]-------------------------------------
5,limit()函数的用法
limit(参数1,[参数2])[code]$d=$m->limit(10,20);[/code]注:两个参数必须为int型的,参数2可为空。
6,select()函数的用法:[code]$m->from('lamp')->where('id<23')->order('id desc')->select();[/code]参数为空,其作用就是将前面from ,where,等函数生成的sql语句进行执行。
如果我想查一下from,where函数生成的sql语名
可:[code]echo $m->from('lamp')->where('id<23')->order('id desc');[/code]三,主键查找
首先强调一点,这里所说的主键并不一定是你数据表里的主键
这个主键是相对与本操作来说的,是可以人为设置的
设置方法:[code]$m->primary_key='name';[/code]这样主键就设置为的字段name上了。如果没有设置,程序默认为数据表的主键。
1,find()函数的用法[code]$d =$m->find('23');[/code]查找主键为23的数据
注:这里也可以对多个数据进行查询,多数据查询时要用数据
,当参数为数组时,输出的数据为*文明用语*数组,参数为非数组时,返回的数据为两维数组
相当于mysql_fetch_assoc()获取的数据。
[code]$item = array('23', '7','13');
$d=$m->find($item);[/code]注:这里输出数据的排序是可以设置的,默认为DESC,原因就是目前常用的排序大多是DESC的,如新闻网站,最新的新闻一般在最上面。
排序设置,操作:[code]$m->order='asc';[/code]不过要用在find()函数之前才有效。这个同样对后面要讲的finAll()函数也有效
2,findAll()的用法
$d=$m->findAll();
参数为空,就是获取表内的全部数据,以主键排序输出
排序设置,前面已经提到[code]$m->order='asc';[/code]要用在findAll()这前,默认为DESC的
四,添加数据
insert()函数用法
insert(array(参数1))[code]$content = array('name'=>'streen003', 'age'=>'23');

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

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

// +---------------------------------------------------------------
// |         Aspire Mode Class
// +---------------------------------------------------------------
// | Copyright (c) 2009 http://www.bc263.com All rights reserved.
// +---------------------------------------------------------------
// | Author: streen003 <streen003gmail.com>
// +---------------------------------------------------------------

class Model {
       
        //定义变量
        private    $table_info;  //数据表信息
        private    $option;      //SQL语句选项
        protected  $primary_key; //数据表主键
        protected  $table_name;  //数据表名
        protected  $table_prefix; //数据表前缀
        protected  $class_name;  //本类的名称
       
        protected  $db;                        //Mysql Server连接
        private    $params;      //config数据库信息
        protected  $table_field;//数据表字段信息
        protected  $myrow;      //返回数据
       
        protected  $order;      //数据列表的排序,用于 ORDER BY
        protected  $quto_content;//函数quote_into()所用来暂存数据的
        protected  static $instance; //用于构建类的singleton模式参数
        protected  $cache_dir;        //缓存文件目录
        protected  $cache;                //缓存开启开关
       
       
        //构造函数,用于初始化运行环境
        public function __construct() {
               
                global $config; //导入数据库连接信息

                if (!$this->db) {
                       
                        if (!$this->params) {
                               
                                $this->params = $config;
                        }
                       
                        $this->db = Db_Mysqli::getInstance($this->params);
                }
               
                $this->cache = 1;
               
                return true;
        }
       
//        +---------------------------------------------------
//        |                        第一部分: 数据表信息处理
//        +---------------------------------------------------

        //清除变量,$string参数为$this->string对象型
        protected function clear($string) {
               
                if($string) {
                       
                        unset($string);
                }
        }
       
       
        //加载$this->table_name
        protected function parse_table_name() {
               
                if(!$this->table_name) {
                       
                        $this->get_table_name();
                }
               
                return $this->table_name;
        }

       
        //加载$this->tabl_info
        protected function parse_table_info() {
               
                if(!$this->table_info) {
                       
                        $this->get_table_info();
                }
               
                return $this->table_info;
        }
       
        //加载$this->primary_key
        protected function parse_table_primarykey() {
               
                if(!$this->primary_key) {
                       
                        $this->get_table_primarykey();
                }
               
                return $this->primary_key;
        }
       
        //加载$this->table_field
        protected function parse_table_field() {
               
                if(!$this->table_field) {
                       
                        $this->get_table_field();
                }
               
                return $this->table_field;
        }
       
        //获取类名
        protected  function get_class_name() {
               
                if (!$this->class_name) {
                       
                        $class_name = get_class($this);
                        $class_name= strtolower($class_name);
                       
                        $this->class_name = $class_name;
                }
               
                return $this->class_name;
        }
       
        //获取数据表前缀
        protected function get_table_prefix() {
               
                if (!$this->table_prefix) {
                       
                        $this->table_prefix = (!empty($this->params['prefix'])) ? $this->params['prefix'] : '';
                }
               
                return $this->table_prefix;
        }
       
        //获取数据表信息
        protected   function get_table_info() {
               
                $this->parse_table_name();
               
                $sql="SHOW FIELDS FROM {$this->table_name}";
               
                $this->table_info = $this->db->get_array($sql);
               
                return $this->table_info;
        }
       
        //获取数据表名
        protected        function get_table_name() {
               
                $this->get_class_name();
               
                $this->get_table_prefix();
               
                $this->table_name = (!empty($this->table_prefix)) ? $this->table_prefix.substr($this->class_name,0,-5) : substr($this->class_name,0,-5);
               
                return $this->table_name;
        }
       
        //cache_file文件生成
        protected function parse_cache_file($name) {
               
                if(!$this->cache_dir) {
                       
                        $this->cache_dir = './Data/';
                }
               
                $this->parse_table_name();
               
                $cache_file = $this->cache_dir.$this->table_name.'_'.$name.'.data.php';
               
                return $cache_file;
        }
       
        //生成缓存文件
        protected function create_cache($name, $data) {
               
                $cache_file = $this->parse_cache_file($name);
               
                $content = '<?php ';
                $content .= '

.$name.'_cache = ';
                $content .= var_export($data,true).';';
                $content .= ' ?>';
               
                //判断cache_dir是否存在,不存在则建立目录
                if (!is_dir($this->cache_dir)) {
                       
                        mkdir($this->cache_dir,'0777');
                }
               
                file_put_contents($cache_file,$content,LOCK_EX);
               
                return true;
        }
       
        //加载缓存文件
        protected function load_cache($name) {
               
                $cache_file = $this->parse_cache_file($name);
               
                include($cache_file);
               
                return ${$name.'_cache'};
               
        }
       
        //获取数据表的主键
        protected  function get_table_primarykey() {
               
                if ( $this->cache && file_exists( $this->parse_cache_file('primarykey') ) ) {
                       
                        $this->primary_key = $this->load_cache('primarykey');
                }
                else {
                       
                        $this->parse_table_info();
               
                        foreach ($this->table_info as $val) {
                       
                                if ($val['Key']=='PRI') {
                                       
                                        $this->primary_key = $val['Field'];
                                }
                        }
                       
                        $this->create_cache('primarykey',$this->primary_key);
                }
               
                return $this->primary_key;
        }
       
        //获取数据表字段信息
        public  function get_table_field() {
               
                if ( $this->cache && file_exists( $this->parse_cache_file('field') ) ) {
                       
                        $this->table_field = $this->load_cache('field');
                }
                else {
                       
                        $this->parse_table_info();
                       
                        $fields = array();
                       
                        foreach ($this->table_info as $val) {
                               
                                $fields[] = $val['Field'];
                        }
                       
                        $this->table_field = $fields;
                       
                        $this->create_cache('field',$this->table_field);
                }
               
                return $this->table_field;
        }

       
//        +---------------------------------------------------
//        |                        第二部分: Select SQL 语句处理
//        +---------------------------------------------------
       
       
        //from('数据表','查询字段')用于处理 SELECT fields FROM table之类的SQL语句部分
        public function from($name, $item=false) {
               
                if (!empty($name)) {
                       
                        $table_str = $this->parse_options($name);
                       
                        $item_str = ($item==true) ? $this->parse_options($item) : '*';
                       
                        $this->clear($this->option['from']);
                       
                        $this->option['from'] = 'SELECT '.$item_str.' FROM '.$table_str;
                       
                        return $this;
                }
                else {
                        return false;
                }
        }
       
       
        //where('查询条件')用于处理 WHERE id=0531 诸如此类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
        public function where($string) {
               
                if (!empty($string)) {
                       
                        $where_str = $this->parse_options($string,true);
                       
                        $this->option['where'] .= ($this->option['where']) ? ' AND '.$where_str : ' WHERE '.$where_str;
                       
                        return $this;
                }
                else {
                        return false;
                }
        }
       
       
        //or_where('查询条件')用于处理 OR WHERE id=0531 诸如此类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
        public function orwhere($string) {
               
                if (!empty($string)) {
                       
                        $or_where_str = $this->parse_options($string,true);
                       
                        $this->option['or_where'] .= ($this->option['or_where']) ? ' AND '.$or_where_str : ' OR '.$or_where_str;
                       
                        return $this;
                }
                else {
                        return false;
                }
        }
       
       
        //order('排列条件')用于处理 ORDER BY post_id ASC 之类的SQL语句部分,注:当参数中含字符串时应先用quote_into()进行转义
        public function order($string) {
               
                if (!empty($string)) {
                       
                        $order_str = $this->parse_options($string);
                       
                        $this->option['order'] .= ($this->option['order']) ? ' AND '.$order_str : ' ORDER BY '.$order_str;
                       
                        return $this;
                }
                else {
                        return false;
                }
        }
       
       
        //limit(10,20)用于处理LIMIT 10, 20之类的SQL语句部分
        public function limit($num1, $num2) {
               
                if(is_int($num1)) {
                       
                        $num1 = trim($num1);
                       
                        $num2 = (is_int($num2)) ? trim($num2) : '';
                       
                        $limit_str = $num2 ? $num1.', '.$num2 : $num1;
                       
                        $this->clear($this->option['limit']);
                       
                        $this->option['limit'] = ' LIMIT '.$limit_str;
                       
                        return $this;
                }
                else {
                       
                        return false;
                }
        }
       
       
        //处理from(),where(),order()等函数参数,特别是对参数为数组的处理
        protected   function parse_options($string, $info=false) {
               
                if (is_array($string)) {
                       
                        $option_str = '';
                        if ($info==true) {
                               
                                foreach ($string as $val) {
                                       
                                        $option_str .= ' '.trim($val).' AND';
                                }
                               
                                $option_str = substr($option_str,0,-3);
                        }
                        else {
                               
                                foreach ($string as $val) {
                                       
                                        $option_str .= ' '.trim($val).',';
                                }
                               
                                $option_str = substr($option_str,0,-1);
                        }
                }
                else {
                       
                        $option_str = trim($string);
                }
               
                return $option_str;
        }
       
       
        //引用参数,用来完成对某个参数加上单引号,方便SQL语句执行,用法:$this->quote_into('id=?', '5');
        public  function quote_into($string, $param) {
               
                if (!empty($string) && !empty($param)) {
                       
                        $string = str_replace('?', '\''.trim($param).'\'', $string);
                       
                        $this->clear($this->quto_content);
                       
                        $this->quto_content = $string;
                       
                        return $this->quto_content;
                }
                else {
                       
                        return false;
                }
        }
       
       
        //组装SQL语句并完成查询,并返回查询结果,用法$this->select();
        public function select() {
               
                if ($this->option['from']) {
                       
                        $sql=$this->option['from'];
                       
                        if ($this->option['where']) {
                               
                                $sql .= $this->option['where'];
                               
                                if ($this->option['or_where']) {
                                       
                                        $sql .= $this->option['or_where'];
                                        unset($this->option['or_where']);
                                }
                               
                                unset($this->option['where']);
                        }
                       
                        if ($this->option['order']) {
                               
                                $sql .=$this->option['order'];
                                unset($this->option['order']);
                        }
                       
                        if ($this->option['limit']) {
                               
                                $sql .= $this->option['limit'];
                        }
                       
                        $this->clear($this->myrow);
                       
                        $this->myrow = $this->db->get_array($sql);
                       
                        return $this->myrow;
                }
                else {
                       
                        return false;
                }
               
        }
       
       
//        +---------------------------------------------------
//        |                        第三部分: Insert, Update, Delete, Find
//        +---------------------------------------------------

       
        //根据主键,获取某个主键的一行信息,主键可以类内设置
        public function find($id) {
               
                if (!empty($id)) {
                       
                        $this->parse_table_primarykey();
                       
                        $this->parse_table_name();
                       
                        if (!$this->order) {
                               
                                $this->order = 'DESC';
                        }
                        else {
                               
                                $this->order = (in_array(strtoupper($this->order), array('ASC','DESC'))) ? strtoupper($this->order) : 'DESC';
                        }
                       
                        $sql = 'SELECT * FROM '.$this->table_name.' WHERE '.$this->primary_key;
                       
                        if (is_array($id)) {
                               
                                $values = $this->parse_options($id);
                                $sql .= ' IN ('.$values.')';
                        }
                        else {
                               
                                $sql .= ' = '.trim($id);
                        }
                       
                        $sql .= ' ORDER BY '.$this->primary_key.' '.$this->order;
                       
                        $this->clear($this->myrow);
                       
                        $this->myrow = is_array($id) ? $this->db->get_array($sql) : $this->db->fetch_row($sql);
                       
                        return $this->myrow;
                }
                else {
                        return false;
                }
        }
       
       
        //根据主键信息,获取数据表全部信息
        public function findAll() {
               
                $this->parse_table_primarykey();
               
                $this->parse_table_name();
               
                if (!$this->order) {
                       
                        $this->order = 'DESC';
                }else {
                       
                        $this->order = (in_array(strtoupper($this->order), array('ASC','DESC'))) ? strtoupper($this->order) : 'DESC';
                }
               
                $sql = 'SELECT * FROM '.$this->table_name.' ORDER BY '.$this->primary_key.' '.$this->order;
               
                $this->clear($this->myrow);
               
                $this->myrow = $this->db->get_array($sql);
               
                return $this->myrow;
        }
       
       
        //根据某一条件,获取一行信息(字段型),注:只是一行信息
        public function fetchRow($where) {
               
                if (!empty($where)) {
                       
                        $this->parse_table_name();
                       
                        $sql = 'SELECT * FROM '.$this->table_name;
                       
                        //处理where SQL语句
                        $this->clear($this->option['where']);
                       
                        $this->where($where);
                       
                        $sql .= $this->option['where'];
                       
                        unset($this->option['where']);
                       
                        $this->clear($this->myrow);
                       
                        $this->myrow = $this->db->fetch_row($sql);
                       
                        $this->myrow = (Object)$this->myrow;
                       
                        //return $this->myrow;
                        return $this;
                }
                else {
                       
                        return false;
                }
        }
       
       
        //新建一行数据,对象型的
        public function createRow() {
               
                $this->clear($this->myrow);
               
                $this->myrow = (object)$this->myrow;
               
                return $this;
        }
       
       
        //向数据表写入一行信息
        public function insert($content) {
               
                if (is_array($content)) {
                       
                        $this->parse_table_name();
                       
                        $this->parse_table_field();
                       
                        $field_str = '';
                        $content_str = '';
                       
                        $key_arr = array_keys($content);
                       
                        //处理所要写入内容的数组的 values 与数据表字段对应顺序
                        foreach ($this->table_field as $val) {
                               
                                $field_str .= ' '.$val.',';
                               
                                if (in_array($val, $key_arr)) {
                                       
                                        $content_str .= ' \''.$content[$val].'\',';
                                }
                                else {
                                       
                                        $content_str .=' NULL,';
                                }
                        }
                       
                        $field_str = substr($field_str, 0, -1);
                        $content_str = substr($content_str, 0, -1);
                       
                        $sql = 'INSERT INTO '.$this->table_name.'('.$field_str.' )'.' VALUES ('.$content_str.')';
                       
                        $this->db->query($sql);
                       
                        return true;
                }
                else {
                        return false;
                }
        }
       
       
        //删除符合一定条件的行数据,注:如果$where中含有字符串,应用$this->qutote_into()进行转义
        public function delete($where) {
               
                $this->parse_table_name();
               
                $this->clear($this->option['where']);
               
                $this->where($where);
               
                $sql = 'DELETE FROM '.$this->table_name.$this->option['where'];
               
                $this->db->query($sql);
               
                return true;
        }
       
       
        //更新一行信息
        public function update($content,$where) {
               
                if (is_array($content)) {
                       
                        $this->parse_table_name();
                       
                        $this->parse_table_field();
                       
                        $content_str = '';
                        foreach ($content as $key=>$val) {
                               
                                if (in_array($key, $this->table_field)) {
                                       
                                        $content_str .= $key.' = \''.$val.'\',';
                                }
                        }
                       
                        $content_str = substr($content_str, 0, -1);
                       
                        $sql = 'UPDATE '.$this->table_name.' SET '.$content_str;
                       
                        $this->clear($this->option['where']);
                               
                        $this->where($where);
                       
                        $sql .= $this->option['where'];
                       
                        unset($this->option['where']);
                       
                        $this->db->query($sql);
                       
                        return true;
                }
                else {
                       
                        return false;
                }
               
        }
       
       
        //保存数据,实质函数为:update,没有ID时则insett,注:对数为对象型
        public function save() {
               
                if (is_object($this->myrow)) {
                       
                        $myrow = (array)$this->myrow;
                       
                        unset($this->myrow);
                       
                        $key_arr = array_keys($myrow);
                       
                        $this->parse_table_primarykey();
                       
                        if(in_array($this->primary_key, $key_arr)) {
                               
                                $where = $this->quote_into($this->primary_key.'=?',$myrow[$this->primary_key]);
                               
                                unset($myrow[$this->primary_key]);
                               
                                $this->update($myrow,$where);
                        }
                        else {
                               
                                $this->insert($myrow);
                        }
                       
                        return true;
                }
                else {
                       
                        return false;
                }
               
        }
       
       
//        +---------------------------------------------------
//        |                        第四部分: __SET(), __DESTRUCT(), __CALL()
//        +---------------------------------------------------

        //根据一个SQL语句获取执行后的全部数据库(字段型)
        public function execute ($sql) {
               
                if (!empty($sql)) {
                       
                        $this->clear($this->myrow);
                       
                        $this->myrow = $this->db->get_array($sql);
                       
                        return $this->myrow;
                }
                else {
                       
                        return false;
                }
        }

        //对类内受保护对象进行赋值
        public function __set($key, $val) {
               
                if (is_object($this->myrow)) {
                       
                        return $this->myrow->$key = $val;
                }
                else {
                       
                        if(in_array($key, array('cache','order','table_name','primary_key','cache_dir'))) {
                               
                                return $this->$key = $val;
                        }
                        else {
                               
                                return false;
                        }
                }
        }
       
        //获得类内一个受保护数据
        public function __get($val) {
               
                if ($val) {
                       
                        return $this->$val;
                }
                else {
                       
                        return false;
                }
               
        }
       
        //析构函数,用于类内程序运行结束后,打扫战场
        public function __destruct() {
               
                $this->clear($this->params);
               
                $this->clear($this->option['where']);
               
                $this->clear($this->option['or_where']);
               
                $this->clear($this->option['order']);
               
                $this->clear($this->myrow);
               
        }
       
        //处理当类外调用不存在的方法
        public function __call($method,array $args) {
               
                echo 'Class Mode method '.$method.' is not exists!<br>The args is:<br>';
                foreach ($args as $val) {
                        echo $val.'<br>';
                }
        }
       
        //用于构建本类的singleton设计模式
        public static function getinstance() {
               
                if (self::$instance == null) {

                        self::$instance = new Model();
                }
               
                return self::$instance;
        }
       
        //直接调用函数,输出内容
        public function __toString() {
               
                if($this->option) {
                       
                        $sql = $this->option['from'].$this->option['where'].$this->option['or_where'].$this->option['order'].$this->option['limit'];
                       
                        return (string)$sql;
                }
                else {
                       
                        return (string)'This is Model Class!';
                }
        }
       
}

?>[/code]
哈哈,不错的
[url=http://bbs.e860.cn/]澄海论坛[/url]
缓存没见到有过期时间的选项   或者数据变化更新的代码

   缓存一次  再也不更新?
缓存没见到有过期时间的选项   或者数据变化更新的代码

   缓存一次  再也不更新?
ccsk 发表于 2009-7-19 11:13
这个手动更新,不是追求效率高吗?
新版本又加了好多新的功能,更加灵活
看了就要回帖!!!
返回列表