Exemplo n.º 1
0
 /**
  * [_query 组合SQL语句 并调用query方法执行 query方法需要被实现]
  * @param  [type] $method [方法类型]
  * @param  string $id     [主键ID 可选]
  * @return [type]         [array]
  */
 protected function _query($method, $id = '', $use_mem = true)
 {
     if (is_int($id)) {
         if (empty($this->db_sql['where'])) {
             $this->db_sql['where'] = ' WHERE ' . $this->pk . '=' . $id;
         } else {
             $this->db_sql['where'] .= ' AND ' . $this->pk . '=' . $id;
         }
     } elseif (!empty($id)) {
         $this->exception('该参数可选,但只能是整数!');
     }
     $sql = $method . ' ' . ($method == 'select' ? $this->db_sql['field'] : '') . ' FROM ' . $this->db_table . $this->parseSql();
     /* 是否使用memcache */
     if ($method == 'select' && defined('MEMCACHE') && MEMCACHE === true && $use_mem) {
         $mem = new MemcacheModel();
         if ($mem->check_connect()) {
             $data = $mem->getCache($sql);
             if ($data) {
                 return $data;
             } else {
                 $result = $this->query($sql, $method);
                 $expire = defined('MEM_EXPIRE') ? MEM_EXPIRE : 180;
                 $mem->addCache($sql, $result, $expire);
                 return $result;
             }
         }
     }
     return $this->query($sql, $method);
 }