Files
MES/core/base/Model.php
T
2026-08-08 18:28:49 +08:00

233 lines
6.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace core\base;
/**
* 模型基类 - 修复版
*/
class Model
{
protected $table = '';
protected $pdo = null;
protected $where = array();
protected $params = array();
protected $order = '';
protected $limit = '';
protected $fields = '*';
protected $lastError = '';
/** 表前缀,统一为 DGZXY_ */
const TABLE_PREFIX = 'DGZXY_';
public function __construct()
{
$this->pdo = \core\db\Db::pdo();
}
/**
* 获取带前缀的完整表名
*/
public function getTable()
{
// 如果已包含前缀则不重复添加
if (strpos($this->table, self::TABLE_PREFIX) === 0) {
return $this->table;
}
return self::TABLE_PREFIX . $this->table;
}
// 设置表名(外部调用 table() 时传入不带前缀的原始表名)
public function table($table)
{
$this->table = $table;
return $this;
}
// 设置查询字段
public function field($fields)
{
$this->fields = $fields;
return $this;
}
// 设置 WHERE 条件
public function where($where = null, $params = array())
{
if ($where !== null) {
$this->where = $where;
$this->params = $params;
}
return $this;
}
// 设置排序 - 修复版
public function order($order)
{
if (is_array($order)) {
// 支持两种格式:
// 1. ['id DESC'] -> "id DESC"
// 2. ['id' => 'DESC'] -> "id DESC"
$parts = [];
foreach ($order as $key => $value) {
if (is_numeric($key)) {
// 格式:['id DESC']
$parts[] = $value;
} else {
// 格式:['id' => 'DESC']
$parts[] = $key . ' ' . $value;
}
}
$this->order = implode(', ', $parts);
} else {
// 字符串格式:'id DESC'
$this->order = $order;
}
return $this;
}
// 设置 LIMIT
public function limit($limit)
{
$this->limit = $limit;
return $this;
}
// 获取单条记录
public function fetch()
{
$sql = $this->buildSelect();
$stmt = $this->pdo->prepare($sql);
$stmt->execute($this->params);
return $stmt->fetch();
}
// 获取所有记录
public function fetchAll()
{
$sql = $this->buildSelect();
$stmt = $this->pdo->prepare($sql);
$stmt->execute($this->params);
return $stmt->fetchAll();
}
// 构建 SELECT 语句
protected function buildSelect()
{
$sql = 'SELECT ' . $this->fields . ' FROM ' . $this->getTable();
if (!empty($this->where)) {
if (is_string($this->where)) {
$sql .= ' WHERE ' . $this->where;
} else if (is_array($this->where)) {
$sql .= ' WHERE ' . implode(' AND ', $this->where);
}
}
if (!empty($this->order)) {
$sql .= ' ORDER BY ' . $this->order;
}
if ($this->limit) {
$sql .= ' LIMIT ' . $this->limit;
}
return $sql;
}
// 获取最后错误信息
public function getError()
{
return $this->lastError;
}
// 添加记录
public function add($data)
{
$fields = implode(', ', array_keys($data));
$placeholders = ':' . implode(', :', array_keys($data));
$sql = 'INSERT INTO ' . $this->getTable() . ' (' . $fields . ') VALUES (' . $placeholders . ')';
$stmt = $this->pdo->prepare($sql);
$result = $stmt->execute($data);
if ($result === false) {
$err = $stmt->errorInfo();
$this->lastError = isset($err[2]) ? $err[2] : 'Unknown error';
}
return $result;
}
// 更新记录
public function update($data)
{
$set = array();
$params = array();
foreach ($data as $key => $value) {
$set[] = $key . ' = :' . $key;
$params[':' . $key] = $value;
}
$sql = 'UPDATE ' . $this->getTable() . ' SET ' . implode(', ', $set);
if (!empty($this->where)) {
if (is_string($this->where)) {
$sql .= ' WHERE ' . $this->where;
} else if (is_array($this->where)) {
$sql .= ' WHERE ' . implode(' AND ', $this->where);
}
}
// 合并 WHERE 参数
foreach ($this->params as $key => $value) {
$params[$key] = $value;
}
$stmt = $this->pdo->prepare($sql);
return $stmt->execute($params);
}
// 删除记录(按 id
public function delete($id)
{
$sql = 'DELETE FROM ' . $this->getTable() . ' WHERE id = :id';
$stmt = $this->pdo->prepare($sql);
return $stmt->execute(array(':id' => $id));
}
// 按 where 条件删除记录
public function deleteWhere()
{
if (empty($this->where)) {
return false;
}
$sql = 'DELETE FROM ' . $this->getTable();
if (is_string($this->where)) {
$sql .= ' WHERE ' . $this->where;
} else if (is_array($this->where)) {
$sql .= ' WHERE ' . implode(' AND ', $this->where);
}
$stmt = $this->pdo->prepare($sql);
$result = $stmt->execute($this->params);
// 清除 where 条件
$this->where = array();
$this->params = array();
return $result;
}
// 执行原生 SQL
public function query($sql, $params = array())
{
$stmt = $this->pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll();
// 释放结果集游标,避免复用同一 PDO 连接时触发 2014 错误
$stmt->closeCursor();
return $rows;
}
// 执行原生 SQL(无返回)
public function execute($sql, $params = array())
{
$stmt = $this->pdo->prepare($sql);
return $stmt->execute($params);
}
}