php 父类Model实现

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;
}
}
}




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值