您的位置:首页 > 编程语言 > PHP开发

phpcms v9二次开发之数据模型类

2013-03-12 12:52 441 查看
系统模型类:model.class.php

数据模型类的位置:/phpcms/libs/classes

phpcms v9二次开发中,我们要经常需要对模块的数据表进行查询、添加、修改和删除数据等操作,所有这些操作都离不开数据模型类model.class.php,它起到开发者与数据表的交互作用。model.class.php里面封装了许多数据表操作的方法,基本上常用的mysql操作语句都能从中找到,但是它又和原生态的mysql语句有所不同,和其它PHP内容管理系统一样,PHPCMS也对原生态的mysql语句进行封装简化,以使它操作起更加方便,为开发者省去了不少麻烦。具体我们来看一下model.class.php代码片断:

01
<?php
02
pc_base::load_sys_class(
'db_factory'
,
''
,
0);
03
class
model
{
04
 
05
...
06
 
07
public
function
__construct()
{
08
if
(!isset(
$this
->db_config[
$this
->db_setting]))
{
09
 
$this
->db_setting
=
'default'
;
10
}
11
$this
->table_name
=
$this
->db_config[
$this
->db_setting][
'tablepre'
].
$this
->table_name;
12
$this
->db_tablepre
=
$this
->db_config[
$this
->db_setting][
'tablepre'
];
13
$this
->db
=db_factory::get_instance(
$this
->db_config)->get_database(
$this
->db_setting);
14
}
15
 
16
/**
17
 
*
执行sql查询
18
 
*
@param $where查询条件[例`name`='$name']
19
 
*
@param $data 需要查询的字段值[例`name`,`gender`,`birthday`]
20
 
*
@param $limit返回结果范围[例:10或10,10 默认为空]
21
 
*
@param $order排序方式[默认按数据库默认方式排序]
22
 
*
@param $group分组方式[默认为空]
23
 
*
@param $key 返回数组按键名排序
24
 
*
@return array查询结果集数组
25
 
*/
26
final
public
function
select(
$where
=
''
,
$data
=
'*'
,
$limit
=
''
,
$order
=
''
,
$group
=
''
,
$key
=
''
)
{
27
if
(
is_array
(
$where
))
$where
=
$this
->sqls(
$where
);
28
return
$this
->db->select(
$data
,
$this
->table_name,
$where
,
$limit
,
$order
,
$group
,
$key
);
29
}
30
...
31
}
如上面的 select语,和mysql的select()有所不同,只要传入相关参数就可以实现mysql要用很多语句才能实现的功能。更多请看model.class.php。

模块模型类:数据表名称+'_model.class.php'

模块模型类的位置:phpcms/model/

在调用model.class.php里面的方法时,需要先把它实例化。每个模型对应一张数据表,每张数据表对应一个模块模型类。现在我们来建一个球队的模块模型表'fbteam',那么它对应的模型类为football_model.class.php,完整代码如下:

01
<?php
02
03
defined(
'IN_PHPCMS'
)
or
exit
(
'No
permission resources.'
);
04
05
pc_base::load_sys_class(
'model'
,
''
,
0);
//加载加载系统模型类model.class.php
06
class
football_model
extends
model
{
//球队模型类football_model.class.php继承系统模型类model.class.php
07
public
$table_name
;
//声明一个数据表变量
08
public
function
__construct()
{
//初始化球队模型类的构造函数
09
10
  
$this
->db_config
=pc_base::load_config(
'database'
);
//加载数据库配置文件database.php
11
12
$this
->db_setting
=
'default'
;
//为数据库配置文件中配置数据库链接池名称,默认为default
13
14
$this
->table_name
=
'fbteam'
;
//'fbteam'为球队数据表的名称
15
16
  
parent::__construct();
//调用父类model.class.php的构造函数,以获取父类的属性方法
17
18
}
19
20
}
21
22
?>
这样我们就建立好了一个球队模块的数据模型类。

本文首发WBlog博客,欢迎转载!转载请注明本文地址,谢谢。

本文地址:http://www.w3note.com/web/18.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: