init
This commit is contained in:
108
app/common/traits/IndexModel.php
Executable file
108
app/common/traits/IndexModel.php
Executable file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\traits;
|
||||
|
||||
use think\Model as BaseModel;
|
||||
use think\Config;
|
||||
|
||||
trait IndexModel {
|
||||
|
||||
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);
|
||||
}
|
||||
//$this->fetchsql(true);
|
||||
$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 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user