Files
orico-official-website-old/app/common/traits/AdminModel.php
2024-10-29 14:04:59 +08:00

209 lines
6.5 KiB
PHP
Executable File
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 app\common\traits;
use think\Model as BaseModel;
use think\Config;
trait AdminModel {
// // 设置完整的数据表(包含前缀)
// protected $table = '';
// /*
// * 数据自动完成指在不需要手动赋值的情况下对字段的值进行处理后写入数据库
// * 系统支持 auto 、 insert 和 update 三个属性可以分别在写入、添加和更新的时候进行字段的自动完成机制auto属性自动完成包含添加和更新操作
// */
// protected $insert = [];
// protected $update = [];
//自定义初始化
// protected function initialize() {
// //需要调用`Model`的`initialize`方法
// parent::initialize();
// }
//
// //自定义初始化
// protected static function init() {
// //TODO:自定义的初始化
// }
public function getPageList($where = null, $order = null, $field = null, $limit = null, $selfpaginate = true) {
//$this->alias('this')->join('table that', 'this.id=that.id', 'LEFT')->join('othertable other', 'other.id=this.id', 'LEFT');
if (is_array($where)) {
$where = array_merge(['stat' => ['eq', '0']], $where);
}
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if (empty($limit)) {
$limit = Config::get('list_rows');
}
if ($selfpaginate) {
$object = $this->paginate($limit);
} else {
$object = $this->paginate($limit, false, [ 'type' => 'bootstrap', 'var_page' => 'page',]);
}
//$object = $this->paginate($limit);
return $object;
}
public function getList($where = null, $order = null, $field = null, $limit = null) {
if (is_array($where)) {
$where = array_merge(['stat' => ['eq', '0']], $where);
}
if ($where) {
$this->where($where);
}
if ($field) {
$this->field($field);
}
if ($order) {
$this->order($order);
}
if ($limit) {
$this->limit($limit);
}
$data = $this->select();
return $data;
}
public function getOneRow($where, $field = null, $order = null, $stat = true) {
$object = $this::get(function($query)use($where, $field, $order, $stat) {
if ($stat) {
$query->where(['stat' => ['eq', '0']]);
}
$query->where('id', $where);
if ($field) {
$query->field($field);
}
if ($order) {
$query->order($order);
}
//$query->fetchsql(true);
});
return $object;
}
public function getRow($where = null, $field = null, $order = null, $fetchsql = false) {
$object = $this::get(function($query)use($where, $field, $order, $fetchsql) {
if (is_numeric($where)) {
$query->where(['stat' => ['eq', '0']])->where('id', $where);
}
if (is_array($where)) {
$where = array_merge(['stat' => ['eq', '0']], $where);
$query->where($where);
}
if ($field) {
$query->field($field);
}
if ($order) {
$query->order($order);
}
//$fetchsql ? $query->fetchsql(true) : '';
});
return $object;
}
public function insertRow($data) {
$object = $this::create($data);
return $object;
}
public function saveRows($data) {
$object = $this->saveAll($data);
return $object;
}
public function updateRow($data = [], $where = [], $field = null) {
$object = $this::update($data, $where, $field);
return $object;
}
public function updateRows($data = [], $where = []) {
if (!empty($where)) {
$result = $this->save($data, function($query)use($where) {
$query->where($where);
});
}
return isset($result) ? $result : false;
}
public function deleteRow($where, $stat = -1) {
//$result = $this::destroy($id);
if (is_array($where)) {
$wheres = $where;
$data['stat'] = $stat;
} else {
$wheres = ['id' => $where];
$data = ['stat' => $stat];
}
$object = $this::update($data, $wheres);
return $object;
}
public function deleteRows($ids, $stat = -1) {
if (isset($ids) && !empty($ids)) {
$result = $this->where(function($query)use($ids) {
$query->where('id', 'in', $ids);
})->update(['stat' => $stat]);
}
return isset($result) ? $result : false;
}
public function destroyRow($id) {
$result = $this::destroy($id);
return $result;
}
public function destroyRows($ids) {
if (isset($ids) && !empty($ids)) {
//$result = $this::destroy($ids);
$result = $this::destroy(function($query)use($ids) {
$query->where('id', 'in', $ids);
});
}
return isset($result) ? $result : false;
}
public function getOtherList($where = null, $order = null, $field = null, $limit = null) {
if ($where) {
$this->where($where);
}
if ($field) {
$this->field($field);
}
if ($order) {
$this->order($order);
}
if ($limit) {
$this->limit($limit);
}
$data = $this->select();
return $data;
}
public function getOtherLists($where = null, $order = null, $field = null, $limit = null) {
//$this->alias('this')->join('table that', 'this.id=that.id', 'LEFT')->join('othertable other', 'other.id=this.id', 'LEFT');
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if (empty($limit)) {
$limit = Config::get('list_rows');
}
$object = $this->paginate($limit);
return $object;
}
}