123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php namespace Dever;
- use Dever;
- class Model
- {
- protected $method = '';
- protected $store;
- protected $partition = false;
- public $config = array();
- public function __construct($table, $app, $store, $partition, $path)
- {
- $project = Project::load($app);
- if ($table) {
- $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'];
- }
- if (isset($this->config['store']) && $store == 'default') {
- $store = $this->config['store'];
- }
- $file = $app . DIRECTORY_SEPARATOR . $table . '.php';
- $this->config['app'] = $app;
- $this->config['table'] = DEVER_PROJECT . '_' . $app . '_' . $table;
- $this->config['load'] = $app . '/' . $table;
- $this->lang();
- } else {
- $this->config['table'] = $table;
- $this->config['struct'] = false;
- }
- }
- if ($partition) {
- $this->partition($partition);
- }
- $this->store = Dever::store($store, $this->partition);
- if (isset($file)) {
- $this->init($path, $store, $file);
- }
- Dever::reset();
- }
- public function partition($partition)
- {
- $setting = Dever::config('setting')['database']['partition'];
- if (is_array($partition)) {
- $this->partition = $partition;
- } elseif (is_string($partition)) {
- $e = '$v=' . $partition . ';';
- eval($e);
- $this->partition = $v;
- } else {
- $this->partition = $setting;
- }
- if ($this->partition) {
- foreach ($this->partition as $k => &$v) {
- $t = Dever::session($k);
- if ($t) {
- $v = $t;
- } elseif (($k == 'database' || $k == 'table') && strstr($v, '(')) {
- $e = '$v=' . $v . ';';
- eval($e);
- }
- }
- $this->partition['create'] = $setting['create'];
- }
- }
- private function lang()
- {
- if (isset($this->config['lang']) && isset($this->config['struct']) && $pack = Config::get('setting')['lang_pack']) {
- foreach ($this->config['lang'] as $lang) {
- if (isset($this->config['struct'][$lang])) {
- foreach ($pack as $key => $value) {
- if (Config::get('setting')['lang'] != $key) {
- $this->config['struct'][$key . '_' . $lang] = $this->config['struct'][$lang];
- }
- }
- }
- }
- }
- }
- private function init($path, $store, $file)
- {
- $path .= DIRECTORY_SEPARATOR . $store;
- $data['index'] = $data['struct'] = 0;
- if ($this->partition) {
- if (isset($this->partition['database']) && $this->partition['database']) {
- if (!$this->partition['create']) {
- $this->config['table'] .= '_' . $this->partition['database'];
- }
- $path .= '/' . $this->partition['database'];
- }
- if (isset($this->partition['table']) && $this->partition['table']) {
- $this->config['table'] .= '_' . $this->partition['table'];
- $file = rtrim($file, '.php') . '_';
- $file .= $this->partition['table'] . '.php';
- }
- if (isset($this->partition['field']) && $this->partition['field']) {
- if (strstr($this->partition['field']['value'], 'date(')) {
- $this->partition['field']['type'] = 'time';
- $e = '$v=' . $this->partition['field']['value'] . ';';
- eval($e);
- $this->partition['field']['value'] = \Dever\Helper\Date::mktime($v);
- }
- $data['field'] = 0;
- }
- }
- $file = File::get($path . DIRECTORY_SEPARATOR . $file);
- if (is_file($file)) {
- $data = include $file;
- }
- foreach ($data as $k => $v) {
- if (isset($this->config[$k])) {
- $num = count($this->config[$k]);
- if ($v != $num) {
- $this->store->$k($this->config, $v);
- $data[$k] = $num;
- file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
- }
- } elseif ($k == 'field' && $v != $this->partition['field']['value']) {
- $this->store->partition($this->config, $this->partition['field']);
- $data['field'] = $this->partition['field']['value'];
- file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
- }
- }
- }
- public function load($param, $set = array(), $lock = false)
- {
- return $this->store->load($this->config['table'], $param, $set, $this->config['struct'], $lock);
- }
- 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';
- } else {
- $set['order'] = 'id desc';
- }
- }
- if (isset($set['num'])) {
- $set['limit'] = Paginator::init($set['num'], $set['page'] ?? 1, function()use($param){return $this->count($param);});
- }
- if ($lock && isset($this->config['type']) && $this->config['type'] == 'myisam') {
- $lock = false;
- }
- $result = $this->store->select($this->config['table'], $param, $set, $this->config['struct'], $lock);
- if (isset($set['num']) && empty($set['page'])) {
- Paginator::status(empty($result));
- }
- return $result;
- }
- public function find($param, $set = array(), $lock = false)
- {
- if (isset($this->partition['where']) && $this->partition['where']) {
- $param = array_merge($this->partition['where'], $param);
- }
- return $this->store->find($this->config['table'], $param, $set, $this->config['struct'], $lock);
- }
- 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 kv($param, $set = array())
- {
- $result = array();
- $data = $this->select($param, $set);
- if ($data) {
- if (empty($set['kv'])) {
- $set['kv'] = array('id', 'name');
- }
- foreach ($data as $k => $v) {
- $result[$v[$set['kv'][0]]] = $v[$set['kv'][1]];
- }
- }
- return $result;
- }
- public function up($param, $data, $lock = false)
- {
- $info = $this->find($param, array(), $lock);
- if ($info) {
- $state = $this->update($info['id'], $data);
- if ($state) {
- return $info['id'];
- }
- return false;
- } else {
- return $this->insert($data);
- }
- }
- public function insert($data)
- {
- 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)
- {
- if ($lock && isset($this->config['struct']['lock'])) {
- $info = $this->find($param, array('col' => 'id,lock'));
- if ($info) {
- $param['lock'] = $info['lock'];
- $data['lock'] = array('+', 1);
- } else {
- return false;
- }
- }
- return $this->store->update($this->config['table'], $param, $data, $this->config['struct']);
- }
- public function delete($param)
- {
- return $this->store->delete($this->config['table'], $param, $this->config['struct']);
- }
- public function begin()
- {
- return $this->store->begin();
- }
- public function commit()
- {
- return $this->store->commit();
- }
- public function rollback()
- {
- return $this->store->rollback();
- }
- public function query($sql, $bind = array(), $page = array())
- {
- if (strpos($sql, '{table}')) {
- $sql = str_replace('{table}', $this->config['table'], $sql);
- }
- if (isset($page['num'])) {
- if (strpos($sql, 'limit')) {
- $temp = explode('limit', $sql);
- $sql = $temp[0];
- }
- $sql .= ' limit ' . Paginator::init($page['num'], $page['page'] ?? 1, function()use($sql, $bind){return $this->queryCount($sql, $bind);});
- }
- $result = $this->store->query($sql, $bind);
- if (isset($page['num']) && empty($page['page'])) {
- $result = $result->fetchAll();
- Paginator::status(empty($result));
- }
- return $result;
- }
- public function queryCount($sql, $bind)
- {
- $sql = mb_strtolower($sql);
- if (strpos($sql, ' from ')) {
- $temp = explode(' from ', $sql);
- }
- if (isset($temp[1])) {
- if (strpos($temp[1], ' order ')) {
- $temp = explode(' order ', $temp[1]);
- $sql = $temp[0];
- } else {
- $sql = $temp[1];
- }
- if (strpos($sql, 'group')) {
- $sql = 'SELECT count(1) as num FROM (SELECT count(1) FROM '.$sql.' ) a ';
- } else {
- $sql = 'SELECT count(1) as num FROM ' . $sql;
- }
- return $this->store->query($sql, $bind)->fetchColumn();
- }
- }
- public function value($key, $value = false, $col = 'id,name')
- {
- if (isset($this->config['struct'][$key]) && $option = Dever::issets($this->config['struct'][$key], 'value')) {
- if (isset($this->config['struct'][$key]['option'])) {
- $option = $this->config['struct'][$key]['option'];
- } else {
- if (is_string($option) && strpos($option, 'Dever') === 0) {
- eval('$option=' . $option . ';');
- } elseif (is_string($option)) {
- $option = Dever::db($option)->select([], ['col' => $col]);
- } elseif (is_array($option) && !isset($option[0])) {
- $temp = $option;
- $option = array();
- $col = explode(',', $col);
- foreach ($temp as $k => $v) {
- $option[] = array($col[0] => $k, $col[1] => $v);
- }
- }
- $this->config['struct'][$key]['option'] = $option;
- }
- if ($value && $option) {
- if (is_array($value) && isset($value[$key])) {
- $value = $value[$key];
- }
- if (strpos($value, ',')) {
- $temp = explode(',', $value);
- $result = array();
- foreach ($temp as $v) {
- $state = Dever::in_array($option, $v);
- if ($state) {
- $result[] = $state;
- }
- }
- return implode('、', $result);
- }
- return Dever::in_array($option, $value);
- }
- return $option;
- }
- return false;
- }
- public function tree($where, $config, $func = false, $set = array())
- {
- $where[$config[0]] = $config[1];
- $data = $this->select($where, $set);
- if ($data) {
- foreach ($data as $k => &$v) {
- if ($func) $v = call_user_func($func, $k, $v);
- $config[1] = $v[$config[2]];
- $child = $this->tree($where, $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])) {
- $method = $this->config['request'][$method];
- $param = array();
- if (isset($method['where'])) {
- $temp = array();
- foreach ($method['where'] as $k => $v) {
- if ($k == 'or' || $k == 'and') {
- foreach ($v as $k1 => $v1) {
- $this->callCreate($data, $temp, $k1, $v1);
- }
- } else {
- $this->callCreate($data, $temp, $k, $v);
- }
- }
- $param[] = $temp;
- }
- if (isset($method['data'])) {
- $temp = array();
- foreach ($method['data'] as $k => $v) {
- $this->callCreate($data, $temp, $k, $v);
- }
- $param[] = $temp;
- }
- if (isset($method['set']) && isset($data[1])) {
- $param[] = array_merge($method['set'], $data[1]);
- }
- $type = $method['type'];
- return $this->$type(...$param);
- }
- return false;
- }
- private function callCreate($data, &$param, $k, $v)
- {
- if (is_array($v)) {
- if (empty($data[0][$k])) {
- if (empty($v[2])) {
- return;
- }
- $data[0][$k] = $v[2];
- }
- $i = $v[0];
- $j = array($v[1], $data[0][$k]);
- } else {
- if (empty($data[0][$k])) {
- $data[0][$k] = $v;
- }
- $i = $k;
- $j = $data[0][$k];
- }
- $this->callCreateParam($param, $i, $j);
- }
- private function callCreateParam(&$param, $i, $j)
- {
- if (isset($param[$i])) {
- $i .= '#';
- return $this->callCreateParam($param, $i, $j);
- }
- $param[$i] = $j;
- }
- }
|