模型实例化
在HeartPHP中,无需单独对表进行单独定义,只需要使用封装好的方法即可,下面来分析如何使用封装方法.。
创建模型类
数据模型必须以 _model来结尾,并且要继承模型基类base_model
if(!defined('IS_HEARTPHP')) exit('Access Denied');
//附件表
class attachment_model extends base_model {
public $table_name = 'attachment';//表名
public $db_setting = 'heartphp';//DB设置 读取配置文件的键值
public $table_prefix = 'heart_';//前缀
public function __construct() {
global $conf;
parent::__construct($conf);
}
}
只需要创建以上代码,就可以对attachment进行操作,对数据模型操作可以在这个文件写相应的方法
实例化模型类和具体操作
$db = D('attachment_model');
//插入数据
$db->insert(array('filed' => 'data'));
//更新数据1
$db->update(array('filed' => 'data'), array('id' => $id));
//更新数据2
$db->update(array('filed' => 'data'), "id=14");
//带分页查询数据 具体操作可以查看后台事例
$db->select_all($fields, $where='', $order='', $group='', $page=0, $pagesize=10);
//查询数据
$db->select($fileds = '', $where = '', $limit = '', $order = '', $group = '');
//查询单条数据
$db->get_one(array('mid' => '20'));
$db->get_one('20');
$db->get_one('mid=20');
//delete data
$db->delete('mid=20');