通过TP取数据的相关操作类 自己写的.和大家分享一个,觉得好的.支持一下
<?php
class SqlService {
protected $templateModule = NULL;
public $_PageShow = NULL;
protected $option = array();
public function Select($tag = array()) {
$this->_PageShow = NULL;
static $_iterateParseCache = array();
$cacheIterateId = md5(serialize($tag));
if ($_iterateParseCache[$cacheIterateId])
return $_iterateParseCache[$cacheIterateId];
$this->templateModule = $this->SetModule($tag['module']);
if (isset($tag['join'])) {
$this->view($tag['join'],
$this->SetviewField($tag['field']));
unset($tag['field']);
}
$this->SetParose($tag);
if (empty($tag['_type']) || $tag['_type'] == "_select") {
$list = $this->_select($tag['pagesize'],
$tag['limit'],
$tag['function']);
} else {
$list = call_user_func(array($this, $tag['_type']),
$tag['field'],
$tag['limit'],
$tag['function']);
}
if (FALSE !== $tag['output']) {
return service("ShowMe")->ShowTable($list,
$this->templateModule,
($tag['_listFields']
? $tag['_listFields']
: "_listFields"));
}
return $list;
}
/**
* 确定模型
* @param type $name
* @return type
*/
public function SetModule($name = "") {
$moduleName = $name
? ($name == "false"
? ""
: $name)
: MODULE_NAME;
return D(ucwords(parse_name($moduleName,
1)));
}
/**
* 对条件过滤
* 过滤规则 : 如本类有的方法,全部交由本类处理,如本类没有的该当 ,全部交由TP的MODEL 类处理
* join 和 table 参数,需要带表前辍
*/
private function SetParose($tag = array()) {
if (is_array($tag) && !empty($tag)) {
if (empty($tag['field']) && method_exists($this->templateModule,
"getSqlFields")) {
$_listFields = isset($tag['_listFields'])
? $tag['_listFields']
: "_listFields";
$tag['field'] = $this->templateModule->getSqlFields($this->templateModule,
$_listFields);
}
$tag['field'] = $this->CheckPK($tag['field']);
$insertPears = array('where', 'alias', 'field', 'order', 'limit', 'page', 'group', 'having', 'union', 'distinct', 'lock', 'relation');
foreach ($tag as $key => $value) {
if (in_array($key,
$insertPears)) {
call_user_func(array($this->templateModule, $key),
$value);
// call_user_func(array(method_exists($this, $key) ? $this : $this->templateModule, $key), $value);
}
}
if (isset($tag['cache'])) {
$key = md5(serialize($tag));
$this->templateModule->cache($key);
}
$this->option = $tag;
}
}
public function getPageVar() {
$tpl = "<div class='row'><div class='pagination'>%s</div></div>";
if (!is_null($this->_PageShow)) {
return sprintf($tpl,
$this->_PageShow);
}
return "";
}
private function _select($Pagesize = TRUE, $limit = 20, $func = FALSE) {
if ($Pagesize) {
$_moduleListCount = $this->templateModule->count("*");
if ($_moduleListCount > 0) {
$Page = SetPage($_moduleListCount,
(int) $Pagesize);
$this->SetParose($this->option);
$list = $this->templateModule->limit($Page->firstRow . ',' . $Page->listRows)->select();
//分页处理 需优化
$this->_PageShow = $Page->show("Admin");
//查询完成后.清空条件.以免影响下次查询
$this->option = NULL;
}
} else {
$list = $this->templateModule->limit($Pagesize)->select();
$this->option = NULL;
}
// dump($this->templateModule->getLastSql());
if (FALSE !== $func && !empty($list)) {
if (method_exists($this->templateModule,
$func))
return call_user_func(array($this->templateModule, $func),
$list);
if (function_exists($func))
return call_user_func($func,
$list);
}
return $list;
}
public function view($join = array(), $viewFields = array()) {
if (!empty($viewFields)) {
$this->templateModule = new ViewModel();
}
$thisViewFields = array_merge($viewFields,
is_string($join)
? array($join)
: $join);
$this->templateModule->setProperty("viewFields",
$thisViewFields);
}
private function SetviewField($Field = array()) {
if (is_string($Field))
$Field = explode(",",
$Field);
return array($this->templateModule->getModelName() => $Field);
}
/**
* 检测字段中是否存在主键 如没有就补上
* @param type $field
* @return type
*/
private function CheckPK($field = array()) {
$pk = $this->templateModule->getPK();
if (is_array($field) && FALSE === array_search($pk,
$field)) {
$field = array_merge(array($pk),
$field);
}
if (is_string($field)) {
$te = explode(",",
$field);
if (FALSE === array_search($pk,
$te)) {
$field = array_merge(array($pk),
$te);
$field = implode(",",
$field);
}
}
return $field;
}
public function _getField($field, $limit = 1000, $function = FALSE) {
$list = $this->templateModule->limit($limit)->getField($field);
if (FALSE !== $function && !empty($list)) {
if (method_exists($this->templateModule,
$function))
return call_user_func(array($this->templateModule, $function),
$list);
if (function_exists($function))
return call_user_func($function,
$list);
}
return $list;
}
}
以下代码是在模板中套用.类式于其他CMS调用模板数据一样.
<?php
class TagLibAdmin extends TagLib {
protected $tags = array(
'list' => array('attr' => 'module,where,order,field,limit,pagesize,cache,group,join,id,key,mod,function', 'level' => 5),
'get' => array("attr" => 'sql,cache,pagesize,limit,show,id,key,mod', 'level' => 3),
'template' => array("attr" => "file", "close" => 0)
);
function __construct() {
parent::__construct();
$this->tablePrefix = C("DB_PREFIX");
}
public function _list($attr, $content) {
static $_iterateParseCache = array();
$cacheIterateId = md5($attr . $content);
/**
* 标签变量解析
*/
$tag = $this->parseTag($this->parseXmlAttr($attr,
'list'));
/**
* 数据获取
*/
$tag = array_merge($tag,
array("output" => FALSE));
$list = service("Sql")->Select($tag);
/**
* 生成模板PHP代码并返回
*/
$key = isset($tag['key']) ? $tag['key'] : "key";
$id = isset($tag['id']) ? $tag['id'] : "list";
$mod = isset($tag['mod']) ? intval($tag['mod']) : 2;
$this->tpl->set($cacheIterateId,
$list);
$parsestr = "";
$parsestr .= '<?php $page = $this->get("page' . $cacheIterateId . '");$_resule = $this->get(\'' . $cacheIterateId . '\');if ($_resule): $' . $key . '=0;';
$parsestr .= 'foreach($_resule as $key=>$' . $id . '):';
$parsestr .= '++$' . $key . ';$mod = ($' . $key . ' % ' . $mod . ' );?>';
$parsestr .= $content . '<?php endforeach; endif;?>';
//模拟内存缓存
$_iterateParseCache[$cacheIterateId] = $parsestr;
return $parsestr;
}
/**
* 模板包含标签
* 格式
* <template file="APP/模块/模板"/>
* @staticvar array $_admintemplateParseCache
* @param type $attr 属性字符串
* @param type $content 标签内容
* @return array
*/
public function _template($attr, $content) {
static $_admintemplateParseCache = array();
$cacheIterateId = md5($attr . $content);
if (isset($_admintemplateParseCache[$cacheIterateId])) {
return $_admintemplateParseCache[$cacheIterateId];
}
//分析Admintemplate标签的标签定义
$tag = $this->parseXmlAttr($attr,
'template');
$file = explode("/",
$tag['file']);
$counts = count($file);
if ($counts < 2) {
return false;
} else if ($counts < 3) {
$file_path = DIRECTORY_SEPARATOR . "Administrator" . DIRECTORY_SEPARATOR . "Tpl" . DIRECTORY_SEPARATOR . $tag['file'];
dump($file_path);
} else {
$file_path = DIRECTORY_SEPARATOR . $file[0] . DIRECTORY_SEPARATOR . "Tpl" . DIRECTORY_SEPARATOR . $file[1] . DIRECTORY_SEPARATOR . $file[2];
}
//模板路径
$TemplatePath = APP_PATH . C("APP_GROUP_PATH") . $file_path . C("TMPL_TEMPLATE_SUFFIX");
//判断模板是否存在
if (!file_exists_case($TemplatePath)) {
return false;
}
//读取内容
$tmplContent = file_get_contents($TemplatePath);
//解析模板内容
$parseStr = $this->tpl->parse($tmplContent);
$_admintemplateParseCache[$cacheIterateId] = $parseStr;
return $_admintemplateParseCache[$cacheIterateId];
}
private function parseTag($tag = array()) {
if (!empty($tag)) {
foreach ($tag as $key => $value) {
$tempValue = str_replace(array("@#_"),
array($this->tablePrefix),
$value);
if (preg_match("/(array|\\$)/i",
$tempValue))
eval("\$tempValue=" . $tempValue . ";");
$newTag[$key] = $tempValue;
}
$tag = $newTag;
}
return $tag;
}
}
?>