phpcmsv9自定义sql语句查询模型实现

时间:2022-01-13 05:47:52

在phpcmsv9中,自定义sql语句查询可不太好实现,传入sql语句查询很容易被内部转入生成一系列莫名其妙的sql语句,比如最佳前缀等等,直接造成sql语句查询错误,在此也提供两种解决办法,1修改底层sql语句实现,这种方法风险较高,全局影响不作推荐。2.自定义万能模型(推荐),顺便奉上万能模型源码:

<?php
/**
* @desc mengdj<mengdj@outlook.com>
*/
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class a963_extend_model extends model {
public function __construct(){
$this->db_config = pc_base::load_config('database');
$this->db_setting = 'default';
parent::__construct();
} public function sql_query($sql) {
if (!empty($this->db_tablepre)) $sql = str_replace('phpcms_', $this->db_tablepre, $sql);
return parent::query($sql);
} public function fetch_next() {
return $this->db->fetch_next();
} //通过SQL语句查询一条结果
public function get_one_by_sql($sql){
$this->sql_query($sql);
$res = $this->fetch_next();
$this->free_result();
return $res;
}
//通过sql语句查询数组
public function get_array_by_sql($sql){
$this->sql_query($sql);
$res = $this->fetch_array();
$this->free_result();
return $res;
} //释放数据库结果资源,调用底层完成
public function free_result() {
$this->db->free_result();
}
}
?>