<?php
// $config= [
// 'DB_HOST'=>'localhost',
// 'DB_USER'=>'root',
// 'DB_PWD'=>'root',
// 'DB_NAME'=>'test',
// 'DB_CHARSET'=>'utf8',
// 'DB_PREFIX'=>'php34_'
// ];
//$m = new Model($config);
// $data = ['age'=>20,'name'=>'老王'];
// $insertId=$m ->table('php34_demo')->insert($data);
// var_dump($insertId);
//var_dump($m->table('php34_demo')->where('id=3')->delete());
// $data = ['age'=>33];
// var_dump($m->table('php34_demo')->where('id=1')->update($data));
// var_dump($m->table('php34_demo')->max('age'));
//var_dump($m->getByName('小红'));
class Model{
protected $host;
protected $user;
protected $pwd;
protected $dbname;
protected $charset;
protected $prefix;
protected $link;
protected $tableName;
protected $sql;
//存放所有查询条件的
protected $options;
//构造方法,对成员变量进行初始化、
function __construct($config){
$this->host = $config['DB_HOST'];
$this->user = $config['DB_USER'];
$this->pwd = $config['DB_PWD'];
$this->dbname = $config['DB_NAME'];
$this->charset = $config['DB_CHARSET'];
$this->prefix = $config['DB_PREFIX'];
//链接数据库
$this->link =$this->connect();
//得到数据表名
$this->tableName = $this->getTableName();
//初始化options数组
$this->initOptions();
}
protected function connect(){
$link = mysqli_connect($this->host,$this->user,$this->pwd);
if(!$link){
die('数据库连接失败');
}
mysqli_select_db($link,$this->dbname);
mysqli_set_charset($link,$this->charset);
return $link;
}
protected function getTableName(){
//1,如何设置了成员变量,那么通过成员变量来得到表名
if(!empty($this->tableName)){
return $this->prefix.$this->tableName;
}
//2.如果没有设置成员变量,通过类名获得
$className = get_class($this);
//user UserModel
$table = strtolower( substr($className, 0,-5));
return $this->prefix.$table;
}
protected function initOptions(){
$arr = ['where','table','field','order','group','having','limit'];
//将options数组中这些键对应的值全部清空
foreach($arr as $value){
$this->options[$value] = '';
//将table默认设置为tableName
if($value == 'table'){
$this->options[$value] = $this->tableName;
}else if($value == 'field'){
$this->options[$value] = '*';
}
}
}
//field方法
function field($field){
//如果不为空,在进行处理
if(!empty($field)){
if(is_string($field)){
$this->options['field']=$field;
}else if(is_array($field)){
$this->options['field']= join(',',$field);
}
}
return $this;
}
//table方法
function table($table){
if(!empty($table)){
$this->options['table'] = $table;
}
return $this;
}
//where
function where($where){
if(!empty($where)){
$this->options['where'] = 'where '. $where;
}
return $this;
}
//group方法
function group($group){
if(!empty($group)){
$this->options['group'] = 'group by '. $group;
}
return $this;
}
//having方法
function having($having){
if(!empty($having)){
$this->options['having'] = 'having '. $having;
}
return $this;
}
//order方法
function order($order){
if(!empty($order)){
$this->options['order'] = 'order by '. $order;
}
return $this;
}
//limit方法
function limit($limit){
if(!empty($limit)){
if(is_string($limit)){
$this->options['limit'] = 'limit '. $limit;
}else if(is_array($limit)){
$this->options['limit'] = 'limit '. join(',',$limit);
}
}
return $this;
}
//select
function select(){
//先写一个带有占位符的sql语句
$sql = 'select %FIELD% from %TABLE% %WHERE% %GROUP% %HAVING% %ORDER% %LIMIT%';
//将options中对应的值依次的替换上面的占位符
$sql = str_replace(['%FIELD%','%TABLE%','%WHERE%','%GROUP%','%HAVING%','%ORDER%','%LIMIT%'],[$this->options['field'],$this->options['table'],$this->options['where'],$this->options['group'],$this->options['having'],$this->options['order'],$this->options['limit']],$sql);
//保存一份sql语句
$this->sql = $sql;
var_dump($sql);
die();
//执行sql语句
return $this->query($sql);
}
//query
function query($sql){
//清空options数组
$this->initOptions();
$result = mysqli_query($this->link,$sql);
if($result && mysqli_affected_rows($this->link)){
while($data = mysqli_fetch_assoc($result)){
$newData[] = $data;
}
}
return $newData;
}
//exec
function __get($name){
if($name == 'sql'){
return $this->sql;
}
return false;
}
//$data关联数组,键是字段名,值是字段值
function insert($data){
//处理字符串问题,两边需要添加单或双引号
$data = $this->parseValue($data);
//提取所有的键,即提取所有的字段
$keys = array_keys($data);
//提取所有的值
$values = array_values($data);
$sql = 'insert into %TABLE%(%FIELD%) values(%VALUES%)';
$sql = str_replace(['%TABLE%','%FIELD%','%VALUES%'], [$this->options['table'],join(',',$keys),join(',',$values)], $sql);
$this->sql = $sql;
return $this->exec($sql,true);
}
function exec($sql,$isInsert = false){
//清空options数组
$this->initOptions();
$result = mysqli_query($this->link,$sql);
if($result && mysqli_affected_rows($this->link)){
//判断是否是插入语句,是就插入
if($isInsert){
return mysqli_insert_id($this->link);
}else{
return mysqli_affected_rows($this->link);
}
}
}
//传递进来一个数组,将数组中值为字符串的两边加上引号
protected function parseValue($data){
foreach($data as $key=>$value){
if(is_string($value)){
$value = '"'.$value.'"';
}
$newData[$key] = $value;
}
return $newData;
}
//删除函数
function delete(){
$sql = 'delete from %TABLE% %WHERE%';
$sql = str_replace(['%TABLE%','%WHERE%'], [$this->options['table'],$this->options['where']], $sql);
$this->sql = $sql;
return $this->exec($sql);
}
//更新函数
//update 表名 set 字段名=字段值,字段名=字段值 where
function update($data){
//处理$data数组中值为字符串加引号的问题
$data = $this->parseValue($data);
$value = $this->parseUpdate($data);
$sql = 'update %TABLE% set %VALUE% %WHERE%';
$sql = str_replace(['%TABLE%','%VALUE%','%WHERE%'], [$this->options['table'],$value,$this->options['where']], $sql);
$this->sql = $sql;
return $this->exec($sql);
}
protected function parseUpdate($data){
foreach ($data as $key => $value) {
$newData[] = $key.'='.$value;
}
return join(',',$newData);
}
function max($field){
$result = $this->field('max('.$field.') as max')->select();
//select 方法返回的事一个二维数组
return $result[0]['max'];
}
function __destruct(){
mysqli_close($this->link);
}
//getByName,getByAge
function __call($name ,$args){
//获取前5个字符
$str = substr($name, 0,5);
$field=strtolower(substr($name,5));
//判断前五个字符是否是getBy
if($str == 'getBy'){
return $this->where($field.'="'.$args[0].'"')->select();
}
return false;
}
}
封装model类
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.要想遍历Model类的属性,首先得通过Runtime来获取该Model类有哪些属性,输出Model的所有属性的...
- 项目中经常遇到数据持久化的问题,直接使用FMDB比较麻烦,我想直接存Model数据源,然后直接建表,存储。 思路是...
- 把RailsCasts中的视频讲的内容总结成文章,每个视频对应一片文章,希望可以帮助到那些想要学习RailsCas...