自定义MVC框架之工具类-模型类
截止目前已经改造了5个类:
这个模型类支持以下功能:
>连贯操作,js叫链式操作,连贯操作的函数可以打乱顺序,最后一个函数必须是执行语句的那个函数,如select, delete, update, add等
如 $db->table( 'user' )->where( 'id=1' )->select()
等等
->支持扩展各种子类模型,命名如: UserModel, 也可以用其他方式的,跟表名称不同的Model命名,如MyUserModel等
<?php
class Model {
protected $dbHost; //主机
protected $dbUser; //用户名
protected $dbPwd; //密码
protected $dbName; //数据库名
protected $prefix; //表前缀
protected $tbName; //表名称
protected $charset; //字符集
protected $link; //数据库连接资源
protected $sql; //拼接的sql语句
protected $options; //sql查询条件
public function __construct( $config ){
$this->dbHost = $config['db_host'];
$this->dbUser = $config['db_user'];
$this->dbPwd = $config['db_pwd'];
$this->dbName = $config['db_name'];
$this->prefix = $config['prefix'];
$this->charset = $config['charset'];
$this->link = $this->connect();
$this->tbName = $this->getTableName();
$this->initOptions();
}
protected function initOptions(){
$sqlKey = [ 'where', 'group', 'having', 'limit', 'order', 'field', 'table' ];
foreach ( $sqlKey as $key => $value ) {
$this->options[$value] = '';
if( $value == 'table' ) {
$this->options[$value] = $this->tbName;
}
}
}
public function table( $tbName ) {
if( !empty( $tbName ) ) {
$this->options['table'] = $tbName;
}
return $this;
}
public 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;
}
public function order( $order ){
if( !empty( $order ) ) {
$this->options['order'] = 'order by ' . $order;
}
return $this;
}
public function having( $having ){
if( !empty( $group ) ) {
$this->options['having'] = 'having ' . $having;
}
return $this;
}
public function group( $group ){
if( !empty( $group ) ) {
$this->options['group'] = 'group by ' . $group;
}
return $this;
}
public function where( $where ){
if( !empty( $where ) ) {
$this->options['where'] = 'where ' . $where;
}
return $this;
}
public function field( $fields ){
if( !empty( $fields ) ) {
if( is_string( $fields ) ) {
$this->options['field'] = $fields;
}else if( is_array( $fields ) ){
$this->options['field'] = join( ',', $fields );
}
}
return $this;
}
public function getTableName(){
if( !empty( $this->tbName ) ) {
return $this->prefix . $this->tbName;
}
$className = get_class( $this );
//UserModel GoodsModel, 获取Model前面的字符串(就是表名称)
$tbName = strtolower( substr( $className, , - ) );
return $this->prefix . $this->tbName;
}
protected function connect(){
$link = mysql_connect( $this->dbHost, $this->dbUser, $this->dbPwd );
if( !$link ){
die( "mysql数据库连接失败:" . mysql_error() );
}
mysql_select_db( $this->dbName, $link );
mysql_query( $this->charset );
return $link;
}
public function select(){
$sql = 'SELECT %FIELD% FROM %TABLE% %WHERE% %GROUP% %HAVING% %ORDER% %LIMIT%';
$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
);
$this->sql = $sql;
return $this->query( $sql );
}
public function __get( $key ){
if( $key == 'sql' ) {
return $this->sql;
}else if( $key == 'prefix' ) {
return $this->prefix;
}
return false;
}
public function query( $sql ){
//执行语句之前,清空原来的options保存的sql语句临时拼接数据
$this->initOptions();
$res = mysql_query( $sql );
$data = array();
if( $res && mysql_num_rows( $res ) ) {
while( $row = mysql_fetch_assoc( $res ) ){
$data[] = $row;
}
}
return $data;
}
public function add( $data ){
$data = $this->parse( $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 );
}
public 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 );
}
public function exec( $sql, $isInsert = false ){
$this->initOptions();
$res = mysql_query( $sql );
if( $res !== false ) {
if( $isInsert ) {
return mysql_insert_id();
}else {
return mysql_affected_rows();
}
}
return false;
}
public function parse( $data ) {
$res = [];
foreach( $data as $k => $v ){
if( is_string( $v ) ) {
$res[$k] = '"' . $v . '"';
}
}
return $res;
}
public function update( $data ) {
$data = $this->parse( $data );
$fieldValue = $this->format( $data );
$sql = 'UPDATE %TABLE% SET %FIELD% %WHERE%';
$sql = str_replace(
[ '%TABLE%', '%FIELD%', '%WHERE%' ],
[ $this->options['table'], $fieldValue, $this->options['where'] ],
$sql
);
$this->sql = $sql;
return $this->exec( $sql );
}
//update ghostwu_user set field = value, where ....
protected function format( $data ){
$res = [];
foreach( $data as $k => $v ) {
$res[] = $k . '=' . $v;
}
return join( ',', $res );
}
public function __destruct(){
mysql_close( $this->link );
}
public function __call( $funcName, $args ) {
$str = substr( $funcName, , );
$field = substr( $funcName, );
if( $str == 'getBy' ) {
echo $args[];
return $this->where( $field . '="' . $args[] . '"' )->select();
}
return false;
}
}
$config = [
'db_host' => 'localhost',
'db_user' => 'root',
'db_pwd' => '',
'prefix' => 'ghostwu_',
'db_name' => 'blog',
'charset' => 'utf8',
];
$db = new Model( $config );
print_r( $db->field( '*' )->table( $db->prefix . 'users' )->getByName( 'zhangsan' ) );
echo $db->sql . PHP_EOL;
//$db->table( $db->prefix . 'users' )->where( 'id=1' )->update( [ 'name' => 'david', 'email' => 'david@gmail.com' ] );
//echo $db->sql . PHP_EOL;
//$db->table( $db->prefix . 'users' )->where( 'id in( 4, 5, 6, 7 )' )->delete();
//echo $db->sql . PHP_EOL;
//$db->table( $db->prefix . 'users' )->add( ['name' => 'zhangsan', 'email' => 'test@test.com'] );
/*
$list = $db->table( $db->prefix . 'users' )->field( [ 'name', 'email' ] )->where( 'id >= 1' )->order( 'id desc' )->limit( [0, 5] )->select();
echo $db->sql . PHP_EOL;
print_r( $list );
*/
?>
自定义MVC框架之工具类-模型类的更多相关文章
- 自定义MVC框架之工具类-图像处理类
截止目前已经改造了4个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 图像处理类: 1,图片加水印处理( ...
- 自定义MVC框架之工具类-文件上传类
截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...
- 自定义MVC框架之工具类-分页类的封装
以前写过一个MVC框架,封装的有点low,经过一段时间的沉淀,打算重新改造下,之前这篇文章封装过一个验证码类. 这次重新改造MVC有几个很大的收获 >全部代码都是用Ubuntu+Vim编写,以前 ...
- Java Web自定义MVC框架详解 (转)
转自:http://blog.csdn.net/jackfrued/article/details/42774459 最近给学生讲Java Web,希望他们能够在学完这部分内容后自己实现一个MVC框架 ...
- 自定义MVC框架
我们在学习自定义MVC框架的时候常常会听到Model1 ,Model2和MVC.那么什么是Model1 什么是Model2什么又是MVC呢? 什么是Model1? Model1就是一种纯jsp开发技术 ...
- Struts2 自定义MVC框架
一.Model1与Model2: Model1:就是一种纯jsp开发技术,将业务逻辑代码和视图渲染代码杂糅在一起. Model2:Model2是在Model1的基础上,将业务逻辑的代码分离开来,单独形 ...
- 第一章 自定义MVC框架
第一章 自定义MVC框架1.1 MVC模式设计 组成:Model:模型,用于数据和业务的处理 View :视图,用于数据的显示 Controller:控制器 ...
- c#自定义ORM框架---(泛型&反射&实体类扩展属性<附带通用增、删、查、改>)
该教材主要是运用到泛型.反射和实体类扩展属性 步骤一.建立扩展属性类 实体类扩展属性要继承Attribute基类完成 [AttributeUsage(AttributeTargets.Property ...
- 潭州课堂25班:Ph201805201 django框架 第六课 模型类增删改查,常用 的查询矣查询条件 (课堂笔记)
在视图函数中写入增删改查的方法 增: 在 urls 中配置路径 : 查: 1: 在后台打印数据 在模型类中添加格式化输出 : QuerySet,反回的是个对象,可以按索引聚会,用 for 循环,, 找 ...
随机推荐
- Python 常用的内置函数
1. str.isdigit( ) 作用:检测字符串是否有数字组成 2. strip( ) 作用:除去首尾指定的字符,包括空格,换行符,不能除去中间的字符 3. slice( ) 作用:以指定参数切割 ...
- Linux Shell常用脚本整理
轮询检测Apache状态并启用钉钉报警◆ #!/bin/bash shell_user="root" shell_domain="apache" shell_l ...
- 深入理解String类
1.String str = "eee" 和String str = new String("eee")的区别 先看一小段代码, public static v ...
- ssh登录时在参数中加入密码的解决方案
在使用ssh登录远程服务器的时候,在执行完ssh user@ip后,要输入登录密码,有时候登录密码记不住,这样以来Ian带来的很多的麻烦,有没有一种在ssh的参数中直接加入密码的方法呢?查看ssh的帮 ...
- centos7 安装cmake
安装cmake之前,记得升级gcc,请参考centos7 升级GCC版本到7.3.0 #shell 太简单,懒得解释 wget https://cmake.org/files/v3.11/cmake- ...
- [Leetcode]123.买卖股票的最佳时机3
[原题链接][https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/] 分析:动态规划+二分法.以第i天为分界线,计 ...
- WebDriver高级应用实例(5)
5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...
- odoo开发笔记--与微信集成
odoo 与微信 https://github.com/JoneXiong/oejia_wx
- (转)WebSphere禁用SSLv3和RC4算法教程
原文:https://www.cnblogs.com/lsdb/p/7126399.html WebSphere经常会报“SSL 3.0 POODLE攻击信息泄露”和"SSL/TLS 受诫礼 ...
- (转)关于python3中staticmethod(静态方法)classmethod(类方法)实例方法的联系和区别
原文:http://dmcoders.com/2017/08/30/pythonclass/ https://zhuanlan.zhihu.com/p/28010894------正确理解Python ...