截止目前已经改造了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' => '[email protected]' ] );
//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' => '[email protected]'] );
/*
$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 );
*/
?>