Skip to content

Commit

Permalink
改进Model类,新增支持连贯操作方式查询数据的特性
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonweicn committed Jun 22, 2018
1 parent 4d57ac8 commit d1deea9
Showing 1 changed file with 197 additions and 0 deletions.
197 changes: 197 additions & 0 deletions MiniFramework/Library/Mini/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ abstract class Model
* @var object
*/
protected $_dbPool;

private $_curDb;

private $_method;

private $_options;

/**
* 构造
Expand Down Expand Up @@ -60,4 +66,195 @@ public function loadDb($key)

return $this->_dbPool[$key];
}

/**
* 设置当前使用的数据库
*
* @param string $key
* @param array $params
* @return bool | \Mini\Model
*/
public function useDb($key, $params = null)
{
$db = $this->loadDb($key);
if ($db == null) {
if ($params != null) {
$this->_curDb = $this->regDb($key, $params);
} else {
return false;
}
} else {
$this->_curDb = $db;
}

return $this;
}

/**
* 注册数据库对象
*
* @param string $key
* @param array $params
* @return object
*/
public function regDb($key, $params)
{
$this->_dbPool[$key] = Db::factory('Mysql', $params);

return $this->_dbPool[$key];
}

/**
* 查询
*
* @throws Exceptions
* @return array
*/
public function select()
{
$this->_method = 'SELECT';
$sql = $this->createSql();
if ($this->_curDb) {
$res = $this->_curDb->query($sql, 'All');
return $res;
} else {
throw new Exceptions('Database is not found.');
}
}

/**
* 设置查询字段
*
* @param string $field
* @return \Mini\Model
*/
public function field($field = null)
{
if ($field != null) {
$this->_options['field'] = trim($field);
}

return $this;
}

/**
* 设置数据表
*
* @param string $table
* @return \Mini\Model
*/
public function table($table = null)
{
if ($table != null) {
$this->_options['table'] = trim($table);
}

return $this;
}

/**
* 设置查询条件
*
* @param string $where
* @return \Mini\Model
*/
public function where($where = null)
{
if ($where != null) {
$this->_options['where'] = trim($where);
}

return $this;
}

/**
* 设置分组
*
* @param unknown $group
* @return \Mini\Model
*/
public function group($group = null)
{
if ($group != null) {
$this->_options['group'] = trim($group);
}

return $this;
}

/**
* 设置排序
*
* @param string $order
* @return \Mini\Model
*/
public function order($order = null)
{
if ($order != null) {
$this->_options['order'] = trim($order);
}

return $this;
}

/**
* 设置LIMIT
*
* @param string $limit
* @return \Mini\Model
*/
public function limit($limit = null)
{
if ($limit != null) {
$this->_options['limit'] = trim($limit);
}

return $this;
}

/**
* 创建SQL语句
*
* @return bool | string
*/
private function createSql()
{
if (! isset($this->_options['table']) || $this->_options['table'] == '') {
return false;
}

$sql = $this->_method;

if (! isset($this->_options['where']) || $this->_options['where'] == '') {
$where = '';
} else {
$where = ' WHERE ' . $this->_options['where'];
}

if ($this->_method == 'SELECT') {

if (! isset($this->_options['field']) || $this->_options['field'] == '') {
$sql .= ' * ';
} else {
$sql .= ' ' . $this->_options['field'] . ' ';
}
$sql .= 'FROM ' . $this->_options['table'] . $where;
if (isset($this->_options['group']) && $this->_options['group'] != '') {
$sql .= ' GROUP BY ' . $this->_options['group'];
}
if (isset($this->_options['order']) && $this->_options['order'] != '') {
$sql .= ' ORDER BY ' . $this->_options['order'];
}
if (isset($this->_options['limit']) && $this->_options['limit'] != '') {
$sql .= ' LIMIT ' . $this->_options['limit'];
}

}

$this->_options = array();

$this->_method = '';

return $sql;
}
}

0 comments on commit d1deea9

Please sign in to comment.