点击(此处)折叠或打开
-
<?php
-
class lms_model {
-
public $table = 'table_name';
-
-
/**
-
* and 条件在前 or 在后
-
*/
-
public $where_array = array(
-
array( 'part_sql' => ' time_day >=? ' , 'key' => 'time_day_start', 'op' => 'AND' , 'type' => 'where') ,
-
array( 'part_sql' => ' time_day <=? ' , 'key' => 'time_day_end', 'op' => 'AND' , 'type' => 'where') ,
-
array( 'part_sql' => ' group by ? ' , 'key' => 'org' , 'type' => 'group') ,
-
array( 'part_sql' => ' order by ? desc ' , 'key' => 'id' , 'type' => 'order') ,
-
array( 'part_sql' => ' limit ? ' , 'key' => 'limit' , 'type' => 'limit')
-
);
-
/**
-
* 拼接sql
-
*/
-
public function get_where($request) {
-
-
if( empty($request) ) {
-
return '';
-
}
-
-
extract($request);
-
-
$sql = $where_sql = $group_sql = $order_sql = $limit_sql = '';
-
-
$bind_data = array();
-
-
//拼接where
-
-
foreach($this->where_array as $k => $v) {
-
if( isset($$v['key']) && $v['type'] == 'where') {
-
$where_sql .= $v['op'] . $v['part_sql'];
-
$bind_data[] = $$v['key'];
-
continue;
-
}
-
-
if( isset($$v['key']) && $v['type'] == 'group') {
-
$group_sql .= $v['part_sql'];
-
$bind_data[] = $$v['key'];
-
continue;
-
}
-
-
if( isset($$v['key']) && $v['type'] == 'order') {
-
$order_sql .= $v['part_sql'];
-
$bind_data[] = $$v['key'];
-
continue;
-
}
-
-
if( isset($$v['key']) && $v['type'] == 'limit') {
-
$limit_sql .= $v['part_sql'];
-
$bind_data[] = $$v['key'];
-
continue;
-
}
-
}
-
-
-
if(!empty($where_sql)) $sql = ' where ' . ltrim($where_sql,"and");
-
-
$sql .= $group_sql;
-
-
$sql .= $order_sql;
-
-
$sql .= $limit_sql;
-
-
return array('sql' => $sql , 'bind_data' => $bind_data);
-
}
-
public function insert() {
-
}
-
public function update() {
-
}
-
public function delete() {
-
}
-
/**
-
* args['field'] default *
-
* args['where'] default null
-
* args['group'] default null
-
* args['order'] default null
-
* args['limit'] default null
-
*/
-
public function get_data($args) {
-
$where = $this->get_where($args);
-
$field='*';
-
if( isset($args['field']) && !empty($args['field']) ) $field = $args['field'];
-
$sql = " SELECT {$field} FROM {$this->table} {$where['sql']}";
-
echo $sql;
-
echo "
";
-
print_r($where['bind_data']);
-
echo "
";
-
}
-
/**
-
* args['field'] default *
-
* args['where'] default null
-
* args['group'] default null
-
* args['order'] default null
-
*/
-
public function get_one($args) {
-
$args['limit'] = 1;
-
$where = $this->get_where($args);
-
$field='*';
-
if( isset($args['field']) && !empty($args['field']) ) $field = $args['field'];
-
$sql = " SELECT {$field} FROM {$this->table} {$where['sql']}";
-
echo $sql;
-
echo "
";
-
print_r($where['bind_data']);
-
echo "
";
-
}
-
}
-
$model = new lms_model();
-
$model->get_data( array('time_day_start' => '20160101','time_day_end' => 21231) );
- $model->get_one( array('time_day_start' => '20160101','time_day_end' => 21231) );