123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- <?php namespace Dever;
- class Model
- {
- protected $method = '';
- protected $store;
- protected $config = array();
- public function __construct($table, $app, $path)
- {
- $store = 'default';
- if (strpos($table, '.')) {
- list($table, $this->method) = explode('.', $table);
- }
- if (strpos($table, ':')) {
- list($table, $store) = explode(':', $table);
- }
- $project = Project::load($app);
- $file = $path . DIRECTORY_SEPARATOR . $table . '.php';
- $base = $project['path'] . $file;
- if (is_file($base)) {
- $this->config = include $base;
- }
- $this->config['name'] = DEVER_PROJECT . '_' . $app . '_' . $table;
- $this->config['app'] = $app;
- if (isset($this->config['store'])) {
- $store = $this->config['store'];
- }
- $setting = Config::get('setting')['database'][$store];
- $class = 'Dever\\Store\\' . $setting['type'];
- $this->store = $class::getInstance($store, $setting);
- $this->load();
- $this->init($file);
- }
- private function load()
- {
- 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($file)
- {
- $data['struct'] = $data['index'] = 0;
- $file = Path::get($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) . ';');
- }
- }
- }
- }
- public function select($param = array(), $set, $call = false)
- {
- return $this->store->select($this->config['name'], $param, $set);
- if ($call) {
- }
- if ($data) {
- if (isset($set['reset'])) {
- $result = $this->reset($data, $set['reset'], $call);
- } else {
- if ($call) {
- foreach ($data as $row) {
- $result[] = $call($row);
- }
- }
- }
- }
- return $result;
- }
- public function find($where, $call = false)
- {
- return $this->db()->find($where, $call);
- }
- public function query($sql, $data = array(), $method = '')
- {
- return $this->db()->exe($sql, $data, $method);
- }
- public function fetch($sql, $data = array(), $cache = false)
- {
- return $this->fetchAll($sql, $data, false, $cache, 'fetch');
- }
- public function fetchAll($sql, $data = array(), $page = array(), $cache = false, $method = 'fetchAll')
- {
- $this->setTable($sql);
- if (!$cache && !empty(Config::get('cache')->mysql)) {
- $cache = 'getSql_' . md5($sql . serialize($data));
- if ($page && $p = Input::get('page')) {
- $cache .= '_p' . $p;
- }
- }
- if ($cache) {
- $result = $this->db()->cache($cache);
- if ($result) {
- return $result;
- }
- }
- if ($page) {
- $result = $this->page($page, $sql, $data);
- } else {
- $result = $this->query($sql, $data)->$method();
- }
-
- if ($cache) {
- $this->db()->cache($cache, $result);
- } else {
- $this->db()->log($this->sql, $data, $result);
- }
- return $result;
- }
- public function rowCount($sql, $data = array())
- {
- return $this->query($sql, $data)->rowcount();
- }
- public function row($sql, $data = array())
- {
- return $this->query($sql, $data)->rowcount();
- }
- public function lastId($sql, $data = array())
- {
- return $this->query($sql, $data)->id();
- }
- public function page($config = array(), $sql = false, $data = array())
- {
- return $this->db()->getPageBySql($config, $sql, $data, $this);
- }
- public function index($index)
- {
- return $this->db()->index($index);
- }
- public function begin()
- {
- return $this->db()->begin();
- }
- public function commit()
- {
- return $this->db()->commit();
- }
- public function rollback()
- {
- return $this->db()->rollback();
- }
- /**
- * __call
- *
- * @return mixd
- */
- public function __call($method, $param)
- {
- if (isset($param[0][0]) && is_array($param[0][0])) {
- $result = array();
- foreach ($param[0] as $k => $v) {
- $result[] = $this->$method($v);
- }
-
- return $result;
- }
- $call = false;
- if (isset($param[1])) {
- if (is_object($param[1])) {
- $call = $param[1];
- } elseif ($method == 'insert') {
- $param[0]['insert_value_num'] = $param[1];
- }
- }
- $param = $param ? $this->initParam($param[0], $method) : array();
- $key = $this->table . $method . md5(serialize($param));
- if (isset($param['clear']) && $param['clear'] && isset($this->data[$key])) {
- unset($this->data[$key]);
- }
- $this->compatible($key, $method, $param);
- if (!isset($this->data[$key])) {
- $this->request($method, $param);
- if (isset($this->config['request'][$method]['type']) && in_array($this->config['request'][$method]['type'], array('update', 'insert'))) {
- return $this->callData($method, $param, $call);
- }
- $this->data[$key] = $this->callData($method, $param, $call);
- }
- return $this->data[$key];
- }
- /**
- * getData
- *
- * @return mixd
- */
- private function callData($method, $param, $call = false)
- {
- $param = $this->search($method, $param);
- $handle = new Model\Handle($method, $this->config, $param, $call);
- return $handle->get();
- }
- /**
- * compatible
- *
- * @return mixd
- */
- protected function compatible($key, $method, $param)
- {
- if (Config::get('database')->compatible && isset($this->config['project'])) {
- $file = $this->config['project']['path'] . 'database/' . ucfirst(Config::get('database')->compatible) . '/' . ucfirst($this->config['name']) . '.php';
- if (is_file($file)) {
- $class = ucfirst($this->config['project']['name']) . '\\Database\\' . ucfirst(Config::get('database')->compatible) . '\\' . ucfirst($this->config['name']);
- if (class_exists($class) && method_exists($class, $method)) {
- $this->data[$key] = $class::$method($param);
- }
- }
- }
- }
- /**
- * request
- *
- * @return mixd
- */
- protected function request($method, $param)
- {
- if (!isset($this->config['struct'])) {
- $this->config['struct'] = array();
- }
- if (empty($this->config['request'][$method]) && isset($this->config['struct'])) {
- $search = '';
- if (isset($param['search_type'])) {
- $search = $param['search_type'];
- }
- $this->config['request'][$method] = Model\Request::get($this->table, $method, $this->config['struct'], $search);
- if ($method == 'list' && isset($this->config['manage']['list_type']) && $this->config['manage']['list_type']) {
- $this->config['request']['list']['page'][0] = 50000;
- }
- if (isset($this->config['request'][$method]['type']) && isset($this->config['request'][$method]['option']) && $this->config['request'][$method]['type'] == 'all' && isset($this->config['request'][$method . '_option'])) {
- $this->config['request'][$method]['option'] = array_merge($this->config['request'][$method]['option'], $this->config['request'][$method . '_option']);
- }
- }
- if (isset($param['config']) && isset($this->config['request'][$method]['config']) && $this->config['request'][$method]['config']) {
- $this->config['request'][$method] = array_merge($this->config['request'][$method], $param['config']);
- unset($param['config']);
- unset($this->config['request'][$method]['config']);
- }
- if (in_array($method, array('all', 'state', 'total', 'list', 'find')) && isset($param['option'])) {
- foreach ($param['option'] as $k => $v) {
- $this->config['request'][$method]['option'][$k] = $v;
- }
- }
- if ($method == 'list') {
- $search = Dever::search_button();
- if ($search) {
- $this->config['request']['list']['group'] = $search[0];
- $this->config['request']['list']['col'] = $search[1];
- }
- }
- if (isset($param['join'])) {
- $this->config['request'][$method]['join'] = $param['join'];
- if (isset($this->config['request'][$method]['option'])) {
- foreach ($this->config['request'][$method]['option'] as $k => $v) {
- if (is_string($v) && !strstr($v, 't_1') && !strstr($k, '-')) {
- $this->config['request'][$method]['option'][$k] = 'yes-t_1.' . $k;
- }
- if (is_array($v) && !strstr($v[0], 't_1')) {
- if (strstr($v[0], '-')) {
- $temp = explode('-', $v[0]);
- $this->config['request'][$method]['option'][$k][0] = 'yes-t_1.' . $temp[1];
- } else {
- $this->config['request'][$method]['option'][$k][0] = 'yes-t_1.' . $k;
- }
- }
- }
- }
- }
- if (DEVER_APP_NAME != 'manage') {
- $lang = Dever::langConfig();
- if (Dever::config('base')->lang && $lang && $lang != Dever::config('base')->lang && isset($this->config['manage']['lang']) && $this->config['manage']['lang']) {
- $col = array();
- foreach ($this->config['manage']['lang'] as $k => $v) {
- $col[] = $lang . '_' . $v . ' as `' . $v . '`';
- }
- if (!isset($this->config['request'][$method]['col'])) {
- $this->config['request'][$method]['col'] = '*';
- }
- $this->config['request'][$method]['col'] .= ',' . implode(',', $col);
- }
- }
- }
- /**
- * search
- *
- * @return mixd
- */
- protected function search($method, $param)
- {
- if (isset($param['search_type']) && $param['search_type'] == 2) {
- $join = array();
- $i = 2;
- foreach ($param as $k => $v) {
- if (strpos($k, '-') !== false) {
- $k = str_replace(array('option_', 'where_'), '', $k);
- if (isset($this->config['struct'][$k]) && isset($this->config['struct'][$k]['sync'])) {
- $temp = explode('-', $k);
- $join[] = array
- (
- 'table' => $temp[0] . '/' . $temp[1],
- 'type' => 'left join',
- 'on' => $this->config['struct'][$k]['sync'],
- );
- $t = 't_' . $i . '.' . $temp[2];
- $this->config['request'][$method]['option'][$t] = $this->config['request'][$method]['option'][$k];
- $param['option_' . $t] = $param['option_' . $k];
- unset($this->config['request'][$method]['option'][$k]);
- unset($param['option_' . $k]);
- $i++;
- }
- }
- }
- if ($join) {
- $this->config['request'][$method]['join'] = $join;
- }
- }
- return $param;
- }
- protected function searchFulltext($k, $param)
- {
- }
- /**
- * initParam
- *
- * @return mixd
- */
- private function initParam($param, $method)
- {
- if ($param && !is_array($param)) {
- if ($this->config['link']) {
- $param = array('id' => $param);
- } else {
- $param = array('where_id' => $param, 'option_id' => $param);
- }
- }
- return $param;
- }
- }
|