手册 >> API参考 >> spModel

spModel

数据库操作类,用户的模型类继承于spModel将可以获得最为常用的数据库操作功能。

成员变量

public

protected

成员函数

public

private

教程

从留言本入门学习spModel

简单的数据表操作教程

常规数据表操作CRUD -- Create/Read/Update/Delete

数据表操作详解

使用SQL语句进行复杂数据库操作

位置

spModel类位于 SP_PATH/Core/spModel.php

函数详细

__construct

构造函数

void __construct( void )

spModel类的构造函数,按应用程序配置进行数据库链接操作。

请注意,如spModel的子类,即是继承于spModel的数据模型类中覆盖了构造函数__construct(),开发者需要在子类的构造函数中调用父类的__construct(),才可以保证数据库的链接有效性。

class mymodel extends spModel{
	function __construct(){
		// 一些操作

		parent::__construct();
	}
}

find

从数据表中查找一条记录

array find(mixed conditions, string sort, string fields)

参数:

返回:

findAll

从数据表中查找记录

array findAll(mixed conditions, string sort, string fields, string limit)

参数:

返回:

findSql

使用SQL语句进行查找操作,等于进行find,findAll等操作

array findSql(sql_string sql)

参数:

返回:

使用findSql()时,请注意:

findBy

按字段值查找一条记录

array findBy(string field, string value)

参数:

返回:

findCount

计算符合条件的记录数量

int findCount(mixed conditions)

参数:

返回:

返回符合$conditions记录的数量,如无任何符合条件的记录将返回 0。

create

在数据表中新增一行数据

int create(array row)

参数:

返回:

新增成功则返回新增的自增量ID,失败则返回FALSE。

createAll

在数据表中新增多条记录

void createAll(array rows)

参数:

update

修改数据,该函数将根据参数中设置的条件而更新表中数据

bool update(mixed conditions, array row)

参数:

返回:

updateField

按字段值修改一条记录

bool updateField(mixed conditions, string field, string value)

参数:

返回:

返回值参考update函数。

delete

按条件删除记录

bool delete(mixed conditions )

参数:

返回:

deleteByPk

按给定的数据表的主键删除记录,该函数简化了delete的操作,根据给定的主键删除对应的记录。

bool deleteByPk(mixed pk)

参数:

返回:

返回值参考delete函数。

query

执行SQL语句,相等于执行新增,修改,删除等操作

bool query(string sql)

参数:

返回:

使用query()时,请注意:

dumpSql

返回最后执行的SQL语句供分析

string dumpSql(void)

__val_escape

过滤转义字符,保证数据库执行语句的安全。

__val_escape()将直接调用数据库驱动的__val_escape()函数进行字符过滤。

string __val_escape(string value)

参数:

返回:

返回通过安全过滤的值。

__call

魔术函数,执行模型扩展类的自动加载及使用

__call()可以让spModel类支持更广泛的用途,如加入spLinker等关联的操作同时具备分页功能。

__prepera_format

私有函数,按表字段调整适合的字段

array __prepera_format(array rows)

参数:

返回:

__prepera_format()将根据当前数据表的字段整理返回适合的字段。