php 父类Model实现
来源:互联网 发布:linux 添加用户组 编辑:程序博客网 时间:2024/05/15 08:59
class Model {
protected $db = null;
protected $table = '';
protected $pk = '';
protected $fields = array();
protected $data = array();
protected $options = array('fields'=>'*' , 'where'=>'','group'=>'' ,
'having'=>'' , 'order'=>'','limit'=>'');
public function __construct() {
$this->table = strtolower(substr( get_called_class() , 0 , -5));
$this->db = MySQL::getIns();
$this->parseTable();
}
public function __set($k , $v) {
$this->data[$k] = $v;
}
public function __get($k) {
return $this->data[$k];
}
/**
* 指定查询条件
* 例:array('cat_id'=>3,'price'=>5)--对应sql条件为cat_id=3 and price=5
*
* @param array $cond
* @return void;
*
*/
public function where($cond=array()) {
if(empty($cond) || !is_array($cond)) {
return $this;
}
$where = ' where 1 ';
foreach($cond as $k=>$v) {
$where .= ' and ' . $k .'=' . "'$v'" ;
}
$this->options['where'] = $where;
return $this;
}
/**
* 指定排序字段
*
* @param string $order
* @return void;
*
*/
public function order($order) {
if(empty($order)) {
return $this;
}
$this->options['order'] = 'order by ' . $order;
return $this;
}
/**
* 指定limit limit(3),limit(5,2)
*
* @param int $offset
* @param int $n
* @return void;
*
*/
public function limit($offset,$n=null) {
if($n === null) {
$n = $offset;
$offset = 0;
}
$this->options['limit'] = "limit $offset , $n";
return $this;
}
/**
* 分析表的字段与主键
*
* @return void;
*/
public function parseTable() {
$sql = 'desc ' . $this->table;
$res = $this->db->mGetAll($sql);
foreach($res as $v) {
$this->fields[] = $v['Field'];
if($v['Key'] === 'PRI') {
$this->pk = $v['Field'];
}
}
}
/**
* 把$options属性里的limit,order,where,拼接成完成sql
*/
public function parseSql() {
$sql = 'select %s from %s %s %s %s %s %s';
$sql = sprintf($sql , $this->options['fields'] , $this->table , $this->options['where'] , $this->options['group'], $this->options['having'] , $this->options['order'], $this->options['limit']);
$this->options = array('fields'=>'*' , 'where'=>'','group'=>'' ,
'having'=>'' , 'order'=>'','limit'=>'');
return $sql;
}
/**
* 查询多行数据
*/
public function select($fields=false) {
if($fields) {
$this->options['fields'] = $fields;
}
$sql = $this->parseSql();
//echo $sql;
return $this->db->mGetAll($sql);
}
/**
* 根据用户的原生sql来查询
*/
public function query($sql) {
return $this->db->mGetAll($sql);
}
/**
* 根据主键查询 1行数据
*
* @param int $id
* @return Array
*/
public function find($id) {
$sql = 'select * from ' . $this->table . ' where ' . $this->pk . '=' . $id;
//echo $sql;
return $this->db->mGetRow($sql);
}
/**
* 根据主键删除行
*
* @param int $id
* @return boolean
*/
public function delete($id=false) {
$sql = 'delete from ' . $this->table . ' where ' .$this->pk . '=' . $id;
return $this->db->mQuery($sql);
}
/**
* 插入1行
*/
public function add($data = array()) {
if(empty($data)) {
if(empty($this->data)) {
$this->create();
}
$data = $this->data;
}
if(empty($data)) {
return false;
}
$this->data = array(); // 重置data属性,防止影响下次调用
return $this->db->mExe($data , $this->table , 'insert');
}
/**
* 把POST中的数据自动写入到data属性中
*/
public function create() {
foreach($_POST as $k=>$v) {
if(in_array($k, $this->fields)) {
$this->data[$k] = $v;
}
}
}
}
protected $db = null;
protected $table = '';
protected $pk = '';
protected $fields = array();
protected $data = array();
protected $options = array('fields'=>'*' , 'where'=>'','group'=>'' ,
'having'=>'' , 'order'=>'','limit'=>'');
public function __construct() {
$this->table = strtolower(substr( get_called_class() , 0 , -5));
$this->db = MySQL::getIns();
$this->parseTable();
}
public function __set($k , $v) {
$this->data[$k] = $v;
}
public function __get($k) {
return $this->data[$k];
}
/**
* 指定查询条件
* 例:array('cat_id'=>3,'price'=>5)--对应sql条件为cat_id=3 and price=5
*
* @param array $cond
* @return void;
*
*/
public function where($cond=array()) {
if(empty($cond) || !is_array($cond)) {
return $this;
}
$where = ' where 1 ';
foreach($cond as $k=>$v) {
$where .= ' and ' . $k .'=' . "'$v'" ;
}
$this->options['where'] = $where;
return $this;
}
/**
* 指定排序字段
*
* @param string $order
* @return void;
*
*/
public function order($order) {
if(empty($order)) {
return $this;
}
$this->options['order'] = 'order by ' . $order;
return $this;
}
/**
* 指定limit limit(3),limit(5,2)
*
* @param int $offset
* @param int $n
* @return void;
*
*/
public function limit($offset,$n=null) {
if($n === null) {
$n = $offset;
$offset = 0;
}
$this->options['limit'] = "limit $offset , $n";
return $this;
}
/**
* 分析表的字段与主键
*
* @return void;
*/
public function parseTable() {
$sql = 'desc ' . $this->table;
$res = $this->db->mGetAll($sql);
foreach($res as $v) {
$this->fields[] = $v['Field'];
if($v['Key'] === 'PRI') {
$this->pk = $v['Field'];
}
}
}
/**
* 把$options属性里的limit,order,where,拼接成完成sql
*/
public function parseSql() {
$sql = 'select %s from %s %s %s %s %s %s';
$sql = sprintf($sql , $this->options['fields'] , $this->table , $this->options['where'] , $this->options['group'], $this->options['having'] , $this->options['order'], $this->options['limit']);
$this->options = array('fields'=>'*' , 'where'=>'','group'=>'' ,
'having'=>'' , 'order'=>'','limit'=>'');
return $sql;
}
/**
* 查询多行数据
*/
public function select($fields=false) {
if($fields) {
$this->options['fields'] = $fields;
}
$sql = $this->parseSql();
//echo $sql;
return $this->db->mGetAll($sql);
}
/**
* 根据用户的原生sql来查询
*/
public function query($sql) {
return $this->db->mGetAll($sql);
}
/**
* 根据主键查询 1行数据
*
* @param int $id
* @return Array
*/
public function find($id) {
$sql = 'select * from ' . $this->table . ' where ' . $this->pk . '=' . $id;
//echo $sql;
return $this->db->mGetRow($sql);
}
/**
* 根据主键删除行
*
* @param int $id
* @return boolean
*/
public function delete($id=false) {
$sql = 'delete from ' . $this->table . ' where ' .$this->pk . '=' . $id;
return $this->db->mQuery($sql);
}
/**
* 插入1行
*/
public function add($data = array()) {
if(empty($data)) {
if(empty($this->data)) {
$this->create();
}
$data = $this->data;
}
if(empty($data)) {
return false;
}
$this->data = array(); // 重置data属性,防止影响下次调用
return $this->db->mExe($data , $this->table , 'insert');
}
/**
* 把POST中的数据自动写入到data属性中
*/
public function create() {
foreach($_POST as $k=>$v) {
if(in_array($k, $this->fields)) {
$this->data[$k] = $v;
}
}
}
}
0 0
- php 父类Model实现
- PHP实现基于mysqli的Model基类完整实例
- php數據庫model
- Model.Classs.php
- 用PHP开始你的MVC(三)实现你的Model层
- 用PHP开始你的MVC(三)实现你的Model层
- 用PHP开始你的MVC(三)实现你的Model层
- 用PHP开始你的MVC(三)实现你的Model层
- 用PHP开始你的MVC(三)实现你的Model层
- 用PHP开始你的MVC(三)实现你的Model层
- liferay实现model监听
- PHP MVC 中的MODEL层
- phpcms model.class.php分析
- PHP面向对象留言板(二)前后端分离,Model实现了实体类与表绑定
- Deformable Part Model 实现细节
- java swing model实现JComboBox
- model/base.php 类库分析
- Model View Controller(MVC) in PHP
- 水平居中,position:relative;left:-50%;
- java单例模式
- Mysql完全卸载
- [leetcode] 166. Fraction to Recurring Decimal 解题报告
- 网络编程知识(6)--用Java编写HTTP服务器
- php 父类Model实现
- _id和ObjectId详解!
- C++ 中ofstream与ifstream用法
- YUV格式浅析
- Vijos 1307-黑皮的正方形【正方形计算】
- Mac自带Apache搭建PHP开发环境(一)phpinfo
- 一致性哈希算法原理设计
- 随机数的生成
- java读取excel