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