|
@@ -10,14 +10,13 @@ class Model
|
|
|
{
|
|
|
$project = Project::load($app);
|
|
|
if ($table) {
|
|
|
- $file = $path . DIRECTORY_SEPARATOR . $table . '.php';
|
|
|
- $base = $project['path'] . $file;
|
|
|
+ $base = $project['path'] . $path . DIRECTORY_SEPARATOR . $table . '.php';
|
|
|
if (is_file($base)) {
|
|
|
$this->config = include $base;
|
|
|
if (isset($this->config['partition']) && empty($partition)) {
|
|
|
$partition = $this->config['partition'];
|
|
|
}
|
|
|
- $file = $path . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . $table . '.php';
|
|
|
+ $file = $app . DIRECTORY_SEPARATOR . $table . '.php';
|
|
|
$this->config['app'] = $app;
|
|
|
$this->config['table'] = DEVER_PROJECT . '_' . $app . '_' . $table;
|
|
|
$this->lang();
|
|
@@ -31,25 +30,30 @@ class Model
|
|
|
}
|
|
|
$this->store = Dever::store($store, $this->partition);
|
|
|
if (isset($file)) {
|
|
|
- $this->init($file);
|
|
|
+ $this->init($path, $store, $file);
|
|
|
}
|
|
|
Dever::reset();
|
|
|
}
|
|
|
- private function partition($partition)
|
|
|
+ public function partition($partition)
|
|
|
{
|
|
|
if (is_array($partition)) {
|
|
|
$this->partition = $partition;
|
|
|
} else {
|
|
|
$this->partition = Dever::config('setting')['database']['partition'] ?? false;
|
|
|
}
|
|
|
- if ($this->partition['database']) {
|
|
|
+ if ($this->partition) {
|
|
|
foreach ($this->partition as $k => &$v) {
|
|
|
$t = Dever::session($k);
|
|
|
- if (!$t) {
|
|
|
+ if ($t) {
|
|
|
+ $v = $t;
|
|
|
+ } else {
|
|
|
$e = '$v=' . $v . ';';
|
|
|
eval($e);
|
|
|
}
|
|
|
}
|
|
|
+ if (empty($this->partition['create'])) {
|
|
|
+ $this->partition['create'] = true;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
private function lang()
|
|
@@ -66,24 +70,31 @@ class Model
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- private function init($file)
|
|
|
+ private function init($path, $store, $file)
|
|
|
{
|
|
|
+ $path .= DIRECTORY_SEPARATOR . $store;
|
|
|
$data['index'] = $data['struct'] = 0;
|
|
|
if ($this->partition) {
|
|
|
- if (isset($this->partition['table'])) {
|
|
|
+ if (isset($this->partition['database']) && $this->partition['database']) {
|
|
|
+ if (!$this->partition['create']) {
|
|
|
+ $this->config['table'] .= '_' . $this->partition['database'];
|
|
|
+ }
|
|
|
+ $path .= '_' . $this->partition['database'] . DIRECTORY_SEPARATOR;
|
|
|
+ }
|
|
|
+ if (isset($this->partition['table']) && $this->partition['table']) {
|
|
|
$this->config['table'] .= '_' . $this->partition['table'];
|
|
|
$file = rtrim($file, '.php') . '_';
|
|
|
- if (isset($this->partition['database'])) {
|
|
|
- $file .= $this->partition['database'] . '_';
|
|
|
- }
|
|
|
$file .= $this->partition['table'] . '.php';
|
|
|
}
|
|
|
- if (isset($this->partition['field'])) {
|
|
|
- $this->partition['field'] = Dever::$date::maketime($this->partition['field']);
|
|
|
+ if (isset($this->partition['field']) && $this->partition['field']) {
|
|
|
+ if (strstr($this->partition['field']['value'], 'date(')) {
|
|
|
+ $this->partition['field']['type'] = 'time';
|
|
|
+ $this->partition['field']['value'] = Dever::$date::maketime($this->partition['field']['value']);
|
|
|
+ }
|
|
|
$data['field'] = 0;
|
|
|
}
|
|
|
}
|
|
|
- $file = Path::get($file);
|
|
|
+ $file = File::get($path . DIRECTORY_SEPARATOR . $file);
|
|
|
if (is_file($file)) {
|
|
|
$data = include $file;
|
|
|
}
|
|
@@ -95,15 +106,18 @@ class Model
|
|
|
$data[$k] = $num;
|
|
|
file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
|
|
|
}
|
|
|
- } elseif ($k == 'field' && $v != $this->partition['field']) {
|
|
|
+ } elseif ($k == 'field' && $v != $this->partition['field']['value']) {
|
|
|
$this->store->partition($this->config, $this->partition['field']);
|
|
|
- $data['field'] = $this->partition['field'];
|
|
|
+ $data['field'] = $this->partition['field']['value'];
|
|
|
file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
public function select($param, $set = array(), $lock = false)
|
|
|
{
|
|
|
+ if (isset($this->partition['where']) && $this->partition['where']) {
|
|
|
+ $param = array_merge($this->partition['where'], $param);
|
|
|
+ }
|
|
|
if (empty($set['order'])) {
|
|
|
if (isset($this->config['order'])) {
|
|
|
$set['order'] = $this->config['order'] . ',id desc';
|
|
@@ -126,6 +140,9 @@ class Model
|
|
|
}
|
|
|
public function count($param = array())
|
|
|
{
|
|
|
+ if (isset($this->partition['where']) && $this->partition['where']) {
|
|
|
+ $param = array_merge($this->partition['where'], $param);
|
|
|
+ }
|
|
|
return $this->store->count($this->config['table'], $param, $this->config['struct']);
|
|
|
}
|
|
|
public function find($param, $set = array(), $lock = false)
|
|
@@ -150,13 +167,13 @@ class Model
|
|
|
{
|
|
|
$info = $this->find($param, array(), $lock);
|
|
|
if ($info) {
|
|
|
- $state = Dever::db('menu')->update($info['id'], $data);
|
|
|
+ $state = $this->update($info['id'], $data);
|
|
|
if ($state) {
|
|
|
return $info['id'];
|
|
|
}
|
|
|
return false;
|
|
|
} else {
|
|
|
- return Dever::db('menu')->insert($data);
|
|
|
+ return $this->insert($data);
|
|
|
}
|
|
|
}
|
|
|
public function insert($data)
|
|
@@ -164,6 +181,9 @@ class Model
|
|
|
if (empty($data['cdate'])) {
|
|
|
$data['cdate'] = DEVER_TIME;
|
|
|
}
|
|
|
+ if (isset($this->partition['where']) && $this->partition['where']) {
|
|
|
+ $data = array_merge($this->partition['where'], $data);
|
|
|
+ }
|
|
|
return $this->store->insert($this->config['table'], $data, $this->config['struct']);
|
|
|
}
|
|
|
public function update($param, $data, $lock = false)
|
|
@@ -237,13 +257,12 @@ class Model
|
|
|
}
|
|
|
public function value($key, $value = false, $col = 'id,name')
|
|
|
{
|
|
|
- if (isset($this->config['struct']) && $option = Dever::isset($this->config['struct'][$key], 'value')) {
|
|
|
+ if (isset($this->config['struct'][$key]) && $option = Dever::isset($this->config['struct'][$key], 'value')) {
|
|
|
if (isset($this->config['struct'][$key]['option'])) {
|
|
|
$option = $this->config['struct'][$key]['option'];
|
|
|
} else {
|
|
|
- if (strpos($option, 'Dever') === 0) {
|
|
|
+ if (is_string($option) && strpos($option, 'Dever') === 0) {
|
|
|
eval('$option=' . $option . ';');
|
|
|
- $value['option'] = $option;
|
|
|
} elseif (is_string($option)) {
|
|
|
$option = Dever::db($option)->select([], ['col' => $col])->fetchAll();
|
|
|
} elseif (is_array($option) && !isset($option[0])) {
|
|
@@ -251,7 +270,7 @@ class Model
|
|
|
$option = array();
|
|
|
$col = explode(',', $col);
|
|
|
foreach ($temp as $k => $v) {
|
|
|
- $option[] = array($col[0] => $k, $col[1] => $name);
|
|
|
+ $option[] = array($col[0] => $k, $col[1] => $v);
|
|
|
}
|
|
|
}
|
|
|
$this->config['struct'][$key]['option'] = $option;
|
|
@@ -261,7 +280,10 @@ class Model
|
|
|
$temp = explode(',', $value);
|
|
|
$result = array();
|
|
|
foreach ($temp as $v) {
|
|
|
- $result[] = Dever::in_array($option, $v);
|
|
|
+ $state = Dever::in_array($option, $v);
|
|
|
+ if ($state) {
|
|
|
+ $result[] = $state;
|
|
|
+ }
|
|
|
}
|
|
|
return implode('、', $result);
|
|
|
}
|
|
@@ -271,6 +293,32 @@ class Model
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
+ public function tree($where, $config, $func = false, $set = array())
|
|
|
+ {
|
|
|
+ $where[$config[0]] = $config[1];
|
|
|
+ $data = $this->select($where, $set)->fetchAll();
|
|
|
+ if ($data) {
|
|
|
+ foreach ($data as &$v) {
|
|
|
+ if ($func) $v = call_user_func($func, $v);
|
|
|
+ $config[1] = $v[$config[2]];
|
|
|
+ $child = $this->tree(array(), $config, $func, $set);
|
|
|
+ if ($child) {
|
|
|
+ $v['children'] = $child;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ public function show($where, $field = 'name', $str = '、')
|
|
|
+ {
|
|
|
+ $result = array();
|
|
|
+ $data = $this->select($where);
|
|
|
+ foreach ($data as $k => $v) {
|
|
|
+ $result[] = $v[$field];
|
|
|
+ }
|
|
|
+ $result = implode($str, $result);
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
public function __call($method, $data)
|
|
|
{
|
|
|
if (isset($this->config['request'][$method])) {
|