12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913 |
- <?php
- namespace Think;
- class Model {
-
- const MODEL_INSERT = 1;
- const MODEL_UPDATE = 2;
- const MODEL_BOTH = 3;
- const MUST_VALIDATE = 1;
- const EXISTS_VALIDATE = 0;
- const VALUE_VALIDATE = 2;
-
- protected $db = null;
-
- private $_db = array();
-
- protected $pk = 'id';
-
- protected $autoinc = false;
-
- protected $tablePrefix = null;
-
- protected $name = '';
-
- protected $dbName = '';
-
- protected $connection = '';
-
- protected $tableName = '';
-
- protected $trueTableName = '';
-
- protected $error = '';
-
- protected $fields = array();
-
- protected $data = array();
-
- protected $options = array();
- protected $_validate = array();
- protected $_auto = array();
- protected $_map = array();
- protected $_scope = array();
-
- protected $autoCheckFields = true;
-
- protected $patchValidate = false;
-
- protected $methods = array('strict','order','alias','having','group','lock','distinct','auto','filter','validate','result','token','index','force');
-
- public function __construct($name='',$tablePrefix='',$connection='') {
-
-
- $this->_initialize();
-
- if(!empty($name)) {
- if(strpos($name,'.')) {
- list($this->dbName,$this->name) = explode('.',$name);
- }else{
- $this->name = $name;
- }
- }elseif(empty($this->name)){
- $this->name = $this->getModelName();
- }
-
- if(is_null($tablePrefix)) {
- $this->tablePrefix = '';
- }elseif('' != $tablePrefix) {
- $this->tablePrefix = $tablePrefix;
- }elseif(!isset($this->tablePrefix)){
- $this->tablePrefix = C('DB_PREFIX');
- }
-
-
-
- $this->db(0,empty($this->connection)?$connection:$this->connection,true);
- }
-
- protected function _checkTableInfo() {
-
-
- if(empty($this->fields)) {
-
- if(C('DB_FIELDS_CACHE')) {
- $db = $this->dbName?:C('DB_NAME');
- $fields = F('_fields/'.strtolower($db.'.'.$this->tablePrefix.$this->name));
- if($fields) {
- $this->fields = $fields;
- if(!empty($fields['_pk'])){
- $this->pk = $fields['_pk'];
- }
- return ;
- }
- }
-
- $this->flush();
- }
- }
-
- public function flush() {
-
- $this->db->setModel($this->name);
- $fields = $this->db->getFields($this->getTableName());
- if(!$fields) {
- return false;
- }
- $this->fields = array_keys($fields);
- unset($this->fields['_pk']);
- foreach ($fields as $key=>$val){
-
- $type[$key] = $val['type'];
- if($val['primary']) {
-
- if (isset($this->fields['_pk']) && $this->fields['_pk'] != null) {
- if (is_string($this->fields['_pk'])) {
- $this->pk = array($this->fields['_pk']);
- $this->fields['_pk'] = $this->pk;
- }
- $this->pk[] = $key;
- $this->fields['_pk'][] = $key;
- } else {
- $this->pk = $key;
- $this->fields['_pk'] = $key;
- }
- if($val['autoinc']) $this->autoinc = true;
- }
- }
-
- $this->fields['_type'] = $type;
-
- if(C('DB_FIELDS_CACHE')){
-
- $db = $this->dbName?:C('DB_NAME');
- F('_fields/'.strtolower($db.'.'.$this->tablePrefix.$this->name),$this->fields);
- }
- }
-
- public function __set($name,$value) {
-
- $this->data[$name] = $value;
- }
-
- public function __get($name) {
- return isset($this->data[$name])?$this->data[$name]:null;
- }
-
- public function __isset($name) {
- return isset($this->data[$name]);
- }
-
- public function __unset($name) {
- unset($this->data[$name]);
- }
-
- public function __call($method,$args) {
- if(in_array(strtolower($method),$this->methods,true)) {
-
- $this->options[strtolower($method)] = $args[0];
- return $this;
- }elseif(in_array(strtolower($method),array('count','sum','min','max','avg'),true)){
-
- $field = isset($args[0])?$args[0]:'*';
- return $this->getField(strtoupper($method).'('.$field.') AS tp_'.$method);
- }elseif(strtolower(substr($method,0,5))=='getby') {
-
- $field = parse_name(substr($method,5));
- $where[$field] = $args[0];
- return $this->where($where)->find();
- }elseif(strtolower(substr($method,0,10))=='getfieldby') {
-
- $name = parse_name(substr($method,10));
- $where[$name] =$args[0];
- return $this->where($where)->getField($args[1]);
- }elseif(isset($this->_scope[$method])){
- return $this->scope($method,$args[0]);
- }else{
- E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
- return;
- }
- }
-
- protected function _initialize() {
-
- }
-
- protected function _facade($data) {
-
- if(!empty($this->fields)) {
- if(!empty($this->options['field'])) {
- $fields = $this->options['field'];
- unset($this->options['field']);
- if(is_string($fields)) {
- $fields = explode(',',$fields);
- }
- }else{
- $fields = $this->fields;
- }
- foreach ($data as $key=>$val){
- if(!in_array($key,$fields,true)){
- if(!empty($this->options['strict'])){
- E(L('_DATA_TYPE_INVALID_').':['.$key.'=>'.$val.']');
- }
- unset($data[$key]);
- }elseif(is_scalar($val)) {
-
- $this->_parseType($data,$key);
- }
- }
- }
-
-
- if(!empty($this->options['filter'])) {
- $data = array_map($this->options['filter'],$data);
- unset($this->options['filter']);
- }
- $this->_before_write($data);
- return $data;
- }
-
- protected function _before_write(&$data) {}
-
- public function add($data='',$options=array(),$replace=false) {
- if(empty($data)) {
-
- if(!empty($this->data)) {
- $data = $this->data;
-
- $this->data = array();
- }else{
- $this->error = L('_DATA_TYPE_INVALID_');
- return false;
- }
- }
-
- $data = $this->_facade($data);
-
- $options = $this->_parseOptions($options);
- if(false === $this->_before_insert($data,$options)) {
- return false;
- }
-
- $result = $this->db->insert($data,$options,$replace);
- if(false !== $result && is_numeric($result)) {
- $pk = $this->getPk();
-
- if (is_array($pk)) return $result;
- $insertId = $this->getLastInsID();
- if($insertId) {
-
- $data[$pk] = $insertId;
- if(false === $this->_after_insert($data,$options)) {
- return false;
- }
- return $insertId;
- }
- if(false === $this->_after_insert($data,$options)) {
- return false;
- }
- }
- return $result;
- }
-
- protected function _before_insert(&$data,$options) {}
-
- protected function _after_insert($data,$options) {}
- public function addAll($dataList,$options=array(),$replace=false){
- if(empty($dataList)) {
- $this->error = L('_DATA_TYPE_INVALID_');
- return false;
- }
-
- foreach ($dataList as $key=>$data){
- $dataList[$key] = $this->_facade($data);
- }
-
- $options = $this->_parseOptions($options);
-
- $result = $this->db->insertAll($dataList,$options,$replace);
- if(false !== $result ) {
- $insertId = $this->getLastInsID();
- if($insertId) {
- return $insertId;
- }
- }
- return $result;
- }
-
- public function selectAdd($fields='',$table='',$options=array()) {
-
- $options = $this->_parseOptions($options);
-
- if(false === $result = $this->db->selectInsert($fields?:$options['field'],$table?:$this->getTableName(),$options)){
-
- $this->error = L('_OPERATION_WRONG_');
- return false;
- }else {
-
- return $result;
- }
- }
-
- public function save($data='',$options=array()) {
- if(empty($data)) {
-
- if(!empty($this->data)) {
- $data = $this->data;
-
- $this->data = array();
- }else{
- $this->error = L('_DATA_TYPE_INVALID_');
- return false;
- }
- }
-
- $data = $this->_facade($data);
- if(empty($data)){
-
- $this->error = L('_DATA_TYPE_INVALID_');
- return false;
- }
-
- $options = $this->_parseOptions($options);
- $pk = $this->getPk();
- if(!isset($options['where']) ) {
-
- if (is_string($pk) && isset($data[$pk])) {
- $where[$pk] = $data[$pk];
- unset($data[$pk]);
- } elseif (is_array($pk)) {
-
- foreach ($pk as $field) {
- if(isset($data[$field])) {
- $where[$field] = $data[$field];
- } else {
-
- $this->error = L('_OPERATION_WRONG_');
- return false;
- }
- unset($data[$field]);
- }
- }
- if(!isset($where)){
-
- $this->error = L('_OPERATION_WRONG_');
- return false;
- }else{
- $options['where'] = $where;
- }
- }
- if(is_array($options['where']) && isset($options['where'][$pk])){
- $pkValue = $options['where'][$pk];
- }
- if(false === $this->_before_update($data,$options)) {
- return false;
- }
- $result = $this->db->update($data,$options);
- if(false !== $result && is_numeric($result)) {
- if(isset($pkValue)) $data[$pk] = $pkValue;
- $this->_after_update($data,$options);
- }
- return $result;
- }
-
- protected function _before_update(&$data,$options) {}
-
- protected function _after_update($data,$options) {}
-
- public function delete($options=array()) {
- $pk = $this->getPk();
- if(empty($options) && empty($this->options['where'])) {
-
- if(!empty($this->data) && isset($this->data[$pk]))
- return $this->delete($this->data[$pk]);
- else
- return false;
- }
- if(is_numeric($options) || is_string($options)) {
-
- if(strpos($options,',')) {
- $where[$pk] = array('IN', $options);
- }else{
- $where[$pk] = $options;
- }
- $options = array();
- $options['where'] = $where;
- }
-
- if (is_array($options) && (count($options) > 0) && is_array($pk)) {
- $count = 0;
- foreach (array_keys($options) as $key) {
- if (is_int($key)) $count++;
- }
- if ($count == count($pk)) {
- $i = 0;
- foreach ($pk as $field) {
- $where[$field] = $options[$i];
- unset($options[$i++]);
- }
- $options['where'] = $where;
- } else {
- return false;
- }
- }
-
- $options = $this->_parseOptions($options);
- if(empty($options['where'])){
-
- return false;
- }
- if(is_array($options['where']) && isset($options['where'][$pk])){
- $pkValue = $options['where'][$pk];
- }
- if(false === $this->_before_delete($options)) {
- return false;
- }
- $result = $this->db->delete($options);
- if(false !== $result && is_numeric($result)) {
- $data = array();
- if(isset($pkValue)) $data[$pk] = $pkValue;
- $this->_after_delete($data,$options);
- }
-
- return $result;
- }
-
- protected function _before_delete($options) {}
-
- protected function _after_delete($data,$options) {}
-
- public function select($options=array()) {
- $pk = $this->getPk();
- if(is_string($options) || is_numeric($options)) {
-
- if(strpos($options,',')) {
- $where[$pk] = array('IN',$options);
- }else{
- $where[$pk] = $options;
- }
- $options = array();
- $options['where'] = $where;
- }elseif (is_array($options) && (count($options) > 0) && is_array($pk)) {
-
- $count = 0;
- foreach (array_keys($options) as $key) {
- if (is_int($key)) $count++;
- }
- if ($count == count($pk)) {
- $i = 0;
- foreach ($pk as $field) {
- $where[$field] = $options[$i];
- unset($options[$i++]);
- }
- $options['where'] = $where;
- } else {
- return false;
- }
- } elseif(false === $options){
- $options['fetch_sql'] = true;
- }
-
- $options = $this->_parseOptions($options);
-
- if(isset($options['cache'])){
- $cache = $options['cache'];
- $key = is_string($cache['key'])?$cache['key']:md5(serialize($options));
- $data = S($key,'',$cache);
- if(false !== $data){
- return $data;
- }
- }
- $resultSet = $this->db->select($options);
- if(false === $resultSet) {
- return false;
- }
- if(!empty($resultSet)) {
- if(is_string($resultSet)){
- return $resultSet;
- }
- $resultSet = array_map(array($this,'_read_data'),$resultSet);
- $this->_after_select($resultSet,$options);
- if(isset($options['index'])){
- $index = explode(',',$options['index']);
- foreach ($resultSet as $result){
- $_key = $result[$index[0]];
- if(isset($index[1]) && isset($result[$index[1]])){
- $cols[$_key] = $result[$index[1]];
- }else{
- $cols[$_key] = $result;
- }
- }
- $resultSet = $cols;
- }
- }
- if(isset($cache)){
- S($key,$resultSet,$cache);
- }
- return $resultSet;
- }
-
- protected function _after_select(&$resultSet,$options) {}
-
- public function buildSql() {
- return '( '.$this->fetchSql(true)->select().' )';
- }
-
- protected function _parseOptions($options=array()) {
- if(is_array($options))
- $options = array_merge($this->options,$options);
- if(!isset($options['table'])){
-
- $options['table'] = $this->getTableName();
- $fields = $this->fields;
- }else{
-
- $fields = $this->getDbFields();
- }
-
- if(!empty($options['alias'])) {
- $options['table'] .= ' '.$options['alias'];
- }
-
- $options['model'] = $this->name;
-
- if(isset($options['where']) && is_array($options['where']) && !empty($fields) && !isset($options['join'])) {
-
- foreach ($options['where'] as $key=>$val){
- $key = trim($key);
- if(in_array($key,$fields,true)){
- if(is_scalar($val)) {
- $this->_parseType($options['where'],$key);
- }
- }elseif(!is_numeric($key) && '_' != substr($key,0,1) && false === strpos($key,'.') && false === strpos($key,'(') && false === strpos($key,'|') && false === strpos($key,'&')){
- if(!empty($this->options['strict'])){
- E(L('_ERROR_QUERY_EXPRESS_').':['.$key.'=>'.$val.']');
- }
- unset($options['where'][$key]);
- }
- }
- }
-
- $this->options = array();
-
- $this->_options_filter($options);
- return $options;
- }
-
- protected function _options_filter(&$options) {}
-
- protected function _parseType(&$data,$key) {
- if(!isset($this->options['bind'][':'.$key]) && isset($this->fields['_type'][$key])){
- $fieldType = strtolower($this->fields['_type'][$key]);
- if(false !== strpos($fieldType,'enum')){
-
- }elseif(false === strpos($fieldType,'bigint') && false !== strpos($fieldType,'int')) {
- $data[$key] = intval($data[$key]);
- }elseif(false !== strpos($fieldType,'float') || false !== strpos($fieldType,'double')){
- $data[$key] = floatval($data[$key]);
- }elseif(false !== strpos($fieldType,'bool')){
- $data[$key] = (bool)$data[$key];
- }
- }
- }
-
- protected function _read_data($data) {
-
- if(!empty($this->_map) && C('READ_DATA_MAP')) {
- foreach ($this->_map as $key=>$val){
- if(isset($data[$val])) {
- $data[$key] = $data[$val];
- unset($data[$val]);
- }
- }
- }
- return $data;
- }
-
- public function find($options=array()) {
- if(is_numeric($options) || is_string($options)) {
- $where[$this->getPk()] = $options;
- $options = array();
- $options['where'] = $where;
- }
-
- $pk = $this->getPk();
- if (is_array($options) && (count($options) > 0) && is_array($pk)) {
-
- $count = 0;
- foreach (array_keys($options) as $key) {
- if (is_int($key)) $count++;
- }
- if ($count == count($pk)) {
- $i = 0;
- foreach ($pk as $field) {
- $where[$field] = $options[$i];
- unset($options[$i++]);
- }
- $options['where'] = $where;
- } else {
- return false;
- }
- }
-
- $options['limit'] = 1;
-
- $options = $this->_parseOptions($options);
-
- if(isset($options['cache'])){
- $cache = $options['cache'];
- $key = is_string($cache['key'])?$cache['key']:md5(serialize($options));
- $data = S($key,'',$cache);
- if(false !== $data){
- $this->data = $data;
- return $data;
- }
- }
- $resultSet = $this->db->select($options);
- if(false === $resultSet) {
- return false;
- }
- if(empty($resultSet)) {
- return null;
- }
- if(is_string($resultSet)){
- return $resultSet;
- }
-
- $data = $this->_read_data($resultSet[0]);
- $this->_after_find($data,$options);
- if(!empty($this->options['result'])) {
- return $this->returnResult($data,$this->options['result']);
- }
- $this->data = $data;
- if(isset($cache)){
- S($key,$data,$cache);
- }
- return $this->data;
- }
-
- protected function _after_find(&$result,$options) {}
- protected function returnResult($data,$type=''){
- if ($type){
- if(is_callable($type)){
- return call_user_func($type,$data);
- }
- switch (strtolower($type)){
- case 'json':
- return json_encode($data);
- case 'xml':
- return xml_encode($data);
- }
- }
- return $data;
- }
-
- public function parseFieldsMap($data,$type=1) {
-
- if(!empty($this->_map)) {
- foreach ($this->_map as $key=>$val){
- if($type==1) {
- if(isset($data[$val])) {
- $data[$key] = $data[$val];
- unset($data[$val]);
- }
- }else{
- if(isset($data[$key])) {
- $data[$val] = $data[$key];
- unset($data[$key]);
- }
- }
- }
- }
- return $data;
- }
-
- public function setField($field,$value='') {
- if(is_array($field)) {
- $data = $field;
- }else{
- $data[$field] = $value;
- }
- return $this->save($data);
- }
-
- public function setInc($field,$step=1,$lazyTime=0) {
- if($lazyTime>0) {
- $condition = $this->options['where'];
- $guid = md5($this->name.'_'.$field.'_'.serialize($condition));
- $step = $this->lazyWrite($guid,$step,$lazyTime);
- if(empty($step)) {
- return true;
- }elseif($step < 0) {
- $step = '-'.$step;
- }
- }
- return $this->setField($field,array('exp',$field.'+'.$step));
- }
-
- public function setDec($field,$step=1,$lazyTime=0) {
- if($lazyTime>0) {
- $condition = $this->options['where'];
- $guid = md5($this->name.'_'.$field.'_'.serialize($condition));
- $step = $this->lazyWrite($guid,-$step,$lazyTime);
- if(empty($step)) {
- return true;
- }elseif($step > 0) {
- $step = '-'.$step;
- }
- }
- return $this->setField($field,array('exp',$field.'-'.$step));
- }
-
- protected function lazyWrite($guid,$step,$lazyTime) {
- if(false !== ($value = S($guid))) {
- if(NOW_TIME > S($guid.'_time')+$lazyTime) {
-
- S($guid,NULL);
- S($guid.'_time',NULL);
- return $value+$step;
- }else{
-
- S($guid,$value+$step);
- return false;
- }
- }else{
- S($guid,$step);
-
- S($guid.'_time',NOW_TIME);
- return false;
- }
- }
-
- public function getField($field,$sepa=null) {
- $options['field'] = $field;
- $options = $this->_parseOptions($options);
-
- if(isset($options['cache'])){
- $cache = $options['cache'];
- $key = is_string($cache['key'])?$cache['key']:md5($sepa.serialize($options));
- $data = S($key,'',$cache);
- if(false !== $data){
- return $data;
- }
- }
- $field = trim($field);
- if(strpos($field,',') && false !== $sepa) {
- if(!isset($options['limit'])){
- $options['limit'] = is_numeric($sepa)?$sepa:'';
- }
- $resultSet = $this->db->select($options);
- if(!empty($resultSet)) {
- if(is_string($resultSet)){
- return $resultSet;
- }
- $_field = explode(',', $field);
- $field = array_keys($resultSet[0]);
- $key1 = array_shift($field);
- $key2 = array_shift($field);
- $cols = array();
- $count = count($_field);
- foreach ($resultSet as $result){
- $name = $result[$key1];
- if(2==$count) {
- $cols[$name] = $result[$key2];
- }else{
- $cols[$name] = is_string($sepa)?implode($sepa,array_slice($result,1)):$result;
- }
- }
- if(isset($cache)){
- S($key,$cols,$cache);
- }
- return $cols;
- }
- }else{
-
- if(true !== $sepa) {
- $options['limit'] = is_numeric($sepa)?$sepa:1;
- }
- $result = $this->db->select($options);
- if(!empty($result)) {
- if(is_string($result)){
- return $result;
- }
- if(true !== $sepa && 1==$options['limit']) {
- $data = reset($result[0]);
- if(isset($cache)){
- S($key,$data,$cache);
- }
- return $data;
- }
- foreach ($result as $val){
- $array[] = $val[$field];
- }
- if(isset($cache)){
- S($key,$array,$cache);
- }
- return $array;
- }
- }
- return null;
- }
-
- public function create($data='',$type='') {
-
- if(empty($data)) {
- $data = I('post.');
- }elseif(is_object($data)){
- $data = get_object_vars($data);
- }
-
- if(empty($data) || !is_array($data)) {
- $this->error = L('_DATA_TYPE_INVALID_');
- return false;
- }
-
- $type = $type?:(!empty($data[$this->getPk()])?self::MODEL_UPDATE:self::MODEL_INSERT);
-
- $data = $this->parseFieldsMap($data,0);
-
- if(isset($this->options['field'])) {
- $fields = $this->options['field'];
- unset($this->options['field']);
- }elseif($type == self::MODEL_INSERT && isset($this->insertFields)) {
- $fields = $this->insertFields;
- }elseif($type == self::MODEL_UPDATE && isset($this->updateFields)) {
- $fields = $this->updateFields;
- }
- if(isset($fields)) {
- if(is_string($fields)) {
- $fields = explode(',',$fields);
- }
-
- if(C('TOKEN_ON')) $fields[] = C('TOKEN_NAME', null, '__hash__');
- foreach ($data as $key=>$val){
- if(!in_array($key,$fields)) {
- unset($data[$key]);
- }
- }
- }
-
- if(!$this->autoValidation($data,$type)) return false;
-
- if(!$this->autoCheckToken($data)) {
- $this->error = L('_TOKEN_ERROR_');
- return false;
- }
-
- if($this->autoCheckFields) {
- $fields = $this->getDbFields();
- foreach ($data as $key=>$val){
- if(!in_array($key,$fields)) {
- unset($data[$key]);
- }elseif(MAGIC_QUOTES_GPC && is_string($val)){
- $data[$key] = stripslashes($val);
- }
- }
- }
-
- $this->autoOperation($data,$type);
-
- $this->data = $data;
-
- return $data;
- }
-
-
- public function autoCheckToken($data) {
-
- if(isset($this->options['token']) && !$this->options['token']) return true;
- if(C('TOKEN_ON')){
- $name = C('TOKEN_NAME', null, '__hash__');
- if(!isset($data[$name]) || !isset($_SESSION[$name])) {
- return false;
- }
-
- list($key,$value) = explode('_',$data[$name]);
- if(isset($_SESSION[$name][$key]) && $value && $_SESSION[$name][$key] === $value) {
- unset($_SESSION[$name][$key]);
- return true;
- }
-
- if(C('TOKEN_RESET')) unset($_SESSION[$name][$key]);
- return false;
- }
- return true;
- }
-
- public function regex($value,$rule) {
- $validate = array(
- 'require' => '/\S+/',
- 'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
- 'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
- 'currency' => '/^\d+(\.\d+)?$/',
- 'number' => '/^\d+$/',
- 'zip' => '/^\d{6}$/',
- 'integer' => '/^[-\+]?\d+$/',
- 'double' => '/^[-\+]?\d+(\.\d+)?$/',
- 'english' => '/^[A-Za-z]+$/',
- );
-
- if(isset($validate[strtolower($rule)]))
- $rule = $validate[strtolower($rule)];
- return preg_match($rule,$value)===1;
- }
-
- private function autoOperation(&$data,$type) {
- if(false === $this->options['auto']){
-
- return $data;
- }
- if(!empty($this->options['auto'])) {
- $_auto = $this->options['auto'];
- unset($this->options['auto']);
- }elseif(!empty($this->_auto)){
- $_auto = $this->_auto;
- }
-
- if(isset($_auto)) {
- foreach ($_auto as $auto){
-
-
- if(empty($auto[2])) $auto[2] = self::MODEL_INSERT;
- if( $type == $auto[2] || $auto[2] == self::MODEL_BOTH) {
- if(empty($auto[3])) $auto[3] = 'string';
- switch(trim($auto[3])) {
- case 'function':
- case 'callback':
- $args = isset($auto[4])?(array)$auto[4]:array();
- if(isset($data[$auto[0]])) {
- array_unshift($args,$data[$auto[0]]);
- }
- if('function'==$auto[3]) {
- $data[$auto[0]] = call_user_func_array($auto[1], $args);
- }else{
- $data[$auto[0]] = call_user_func_array(array(&$this,$auto[1]), $args);
- }
- break;
- case 'field':
- $data[$auto[0]] = $data[$auto[1]];
- break;
- case 'ignore':
- if($auto[1]===$data[$auto[0]])
- unset($data[$auto[0]]);
- break;
- case 'string':
- default:
- $data[$auto[0]] = $auto[1];
- }
- if(isset($data[$auto[0]]) && false === $data[$auto[0]] ) unset($data[$auto[0]]);
- }
- }
- }
- return $data;
- }
-
- protected function autoValidation($data,$type) {
- if(false === $this->options['validate'] ){
-
- return true;
- }
- if(!empty($this->options['validate'])) {
- $_validate = $this->options['validate'];
- unset($this->options['validate']);
- }elseif(!empty($this->_validate)){
- $_validate = $this->_validate;
- }
-
- if(isset($_validate)) {
- if($this->patchValidate) {
- $this->error = array();
- }
- foreach($_validate as $key=>$val) {
-
-
-
- if(empty($val[5]) || ( $val[5]== self::MODEL_BOTH && $type < 3 ) || $val[5]== $type ) {
- if(0==strpos($val[2],'{%') && strpos($val[2],'}'))
-
- $val[2] = L(substr($val[2],2,-1));
- $val[3] = isset($val[3])?$val[3]:self::EXISTS_VALIDATE;
- $val[4] = isset($val[4])?$val[4]:'regex';
-
- switch($val[3]) {
- case self::MUST_VALIDATE:
- if(false === $this->_validationField($data,$val))
- return false;
- break;
- case self::VALUE_VALIDATE:
- if('' != trim($data[$val[0]]))
- if(false === $this->_validationField($data,$val))
- return false;
- break;
- default:
- if(isset($data[$val[0]]))
- if(false === $this->_validationField($data,$val))
- return false;
- }
- }
- }
-
- if(!empty($this->error)) return false;
- }
- return true;
- }
-
- protected function _validationField($data,$val) {
- if($this->patchValidate && isset($this->error[$val[0]]))
- return ;
- if(false === $this->_validationFieldItem($data,$val)){
- if($this->patchValidate) {
- $this->error[$val[0]] = $val[2];
- }else{
- $this->error = $val[2];
- return false;
- }
- }
- return ;
- }
-
- protected function _validationFieldItem($data,$val) {
- switch(strtolower(trim($val[4]))) {
- case 'function':
- case 'callback':
- $args = isset($val[6])?(array)$val[6]:array();
- if(is_string($val[0]) && strpos($val[0], ','))
- $val[0] = explode(',', $val[0]);
- if(is_array($val[0])){
-
- foreach($val[0] as $field)
- $_data[$field] = $data[$field];
- array_unshift($args, $_data);
- }else{
- array_unshift($args, $data[$val[0]]);
- }
- if('function'==$val[4]) {
- return call_user_func_array($val[1], $args);
- }else{
- return call_user_func_array(array(&$this, $val[1]), $args);
- }
- case 'confirm':
- return $data[$val[0]] == $data[$val[1]];
- case 'unique':
- if(is_string($val[0]) && strpos($val[0],','))
- $val[0] = explode(',',$val[0]);
- $map = array();
- if(is_array($val[0])) {
-
- foreach ($val[0] as $field)
- $map[$field] = $data[$field];
- }else{
- $map[$val[0]] = $data[$val[0]];
- }
- $pk = $this->getPk();
- if(!empty($data[$pk]) && is_string($pk)) {
- $map[$pk] = array('neq',$data[$pk]);
- }
- if($this->where($map)->find()) return false;
- return true;
- default:
- return $this->check($data[$val[0]],$val[1],$val[4]);
- }
- }
-
- public function check($value,$rule,$type='regex'){
- $type = strtolower(trim($type));
- switch($type) {
- case 'in':
- case 'notin':
- $range = is_array($rule)? $rule : explode(',',$rule);
- return $type == 'in' ? in_array($value ,$range) : !in_array($value ,$range);
- case 'between':
- case 'notbetween':
- if (is_array($rule)){
- $min = $rule[0];
- $max = $rule[1];
- }else{
- list($min,$max) = explode(',',$rule);
- }
- return $type == 'between' ? $value>=$min && $value<=$max : $value<$min || $value>$max;
- case 'equal':
- case 'notequal':
- return $type == 'equal' ? $value == $rule : $value != $rule;
- case 'length':
- $length = mb_strlen($value,'utf-8');
- if(strpos($rule,',')) {
- list($min,$max) = explode(',',$rule);
- return $length >= $min && $length <= $max;
- }else{
- return $length == $rule;
- }
- case 'expire':
- list($start,$end) = explode(',',$rule);
- if(!is_numeric($start)) $start = strtotime($start);
- if(!is_numeric($end)) $end = strtotime($end);
- return NOW_TIME >= $start && NOW_TIME <= $end;
- case 'ip_allow':
- return in_array(get_client_ip(),explode(',',$rule));
- case 'ip_deny':
- return !in_array(get_client_ip(),explode(',',$rule));
- case 'regex':
- default:
-
- return $this->regex($value,$rule);
- }
- }
-
- public function procedure($sql, $parse = false) {
- return $this->db->procedure($sql, $parse);
- }
-
- public function query($sql,$parse=false) {
- if(!is_bool($parse) && !is_array($parse)) {
- $parse = func_get_args();
- array_shift($parse);
- }
- $sql = $this->parseSql($sql,$parse);
- return $this->db->query($sql);
- }
-
- public function execute($sql,$parse=false) {
- if(!is_bool($parse) && !is_array($parse)) {
- $parse = func_get_args();
- array_shift($parse);
- }
- $sql = $this->parseSql($sql,$parse);
- return $this->db->execute($sql);
- }
-
- protected function parseSql($sql,$parse) {
-
- if(true === $parse) {
- $options = $this->_parseOptions();
- $sql = $this->db->parseSql($sql,$options);
- }elseif(is_array($parse)){
- $parse = array_map(array($this->db,'escapeString'),$parse);
- $sql = vsprintf($sql,$parse);
- }else{
- $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix));
- $prefix = $this->tablePrefix;
- $sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $sql);
- }
- $this->db->setModel($this->name);
- return $sql;
- }
-
- public function db($linkNum='',$config='',$force=false) {
- if('' === $linkNum && $this->db) {
- return $this->db;
- }
- if(!isset($this->_db[$linkNum]) || $force ) {
-
- if(!empty($config) && is_string($config) && false === strpos($config,'/')) {
- $config = C($config);
- }
- $this->_db[$linkNum] = Db::getInstance($config);
- }elseif(NULL === $config){
- $this->_db[$linkNum]->close();
- unset($this->_db[$linkNum]);
- return ;
- }
-
- $this->db = $this->_db[$linkNum];
- $this->_after_db();
-
- if(!empty($this->name) && $this->autoCheckFields) $this->_checkTableInfo();
- return $this;
- }
-
- protected function _after_db() {}
-
- public function getModelName() {
- if(empty($this->name)){
- $name = substr(get_class($this),0,-strlen(C('DEFAULT_M_LAYER')));
- if ( $pos = strrpos($name,'\\') ) {
- $this->name = substr($name,$pos+1);
- }else{
- $this->name = $name;
- }
- }
- return $this->name;
- }
-
- public function getTableName() {
- if(empty($this->trueTableName)) {
- $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
- if(!empty($this->tableName)) {
- $tableName .= $this->tableName;
- }else{
- $tableName .= parse_name($this->name);
- }
- $this->trueTableName = strtolower($tableName);
- }
- return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName;
- }
-
- public function startTrans() {
- $this->commit();
- $this->db->startTrans();
- return ;
- }
-
- public function commit() {
- return $this->db->commit();
- }
-
- public function rollback() {
- return $this->db->rollback();
- }
-
- public function getError(){
- return $this->error;
- }
-
- public function getDbError() {
- return $this->db->getError();
- }
-
- public function getLastInsID() {
- return $this->db->getLastInsID();
- }
-
- public function getLastSql() {
- return $this->db->getLastSql($this->name);
- }
-
- public function _sql(){
- return $this->getLastSql();
- }
-
- public function getPk() {
- return $this->pk;
- }
-
- public function getDbFields(){
- if(isset($this->options['table'])) {
- if(is_array($this->options['table'])){
- $table = key($this->options['table']);
- }else{
- $table = $this->options['table'];
- if(strpos($table,')')){
-
- return false;
- }
- }
- $fields = $this->db->getFields($table);
- return $fields ? array_keys($fields) : false;
- }
- if($this->fields) {
- $fields = $this->fields;
- unset($fields['_type'],$fields['_pk']);
- return $fields;
- }
- return false;
- }
-
- public function data($data=''){
- if('' === $data && !empty($this->data)) {
- return $this->data;
- }
- if(is_object($data)){
- $data = get_object_vars($data);
- }elseif(is_string($data)){
- parse_str($data,$data);
- }elseif(!is_array($data)){
- E(L('_DATA_TYPE_INVALID_'));
- }
- $this->data = $data;
- return $this;
- }
-
- public function table($table) {
- $prefix = $this->tablePrefix;
- if(is_array($table)) {
- $this->options['table'] = $table;
- }elseif(!empty($table)) {
-
- $table = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $table);
- $this->options['table'] = $table;
- }
- return $this;
- }
-
- public function using($using){
- $prefix = $this->tablePrefix;
- if(is_array($using)) {
- $this->options['using'] = $using;
- }elseif(!empty($using)) {
-
- $using = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $using);
- $this->options['using'] = $using;
- }
- return $this;
- }
-
- public function join($join,$type='INNER') {
- $prefix = $this->tablePrefix;
- if(is_array($join)) {
- foreach ($join as $key=>&$_join){
- $_join = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $_join);
- $_join = false !== stripos($_join,'JOIN')? $_join : $type.' JOIN ' .$_join;
- }
- $this->options['join'] = $join;
- }elseif(!empty($join)) {
-
- $join = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $join);
- $this->options['join'][] = false !== stripos($join,'JOIN')? $join : $type.' JOIN '.$join;
- }
- return $this;
- }
-
- public function union($union,$all=false) {
- if(empty($union)) return $this;
- if($all) {
- $this->options['union']['_all'] = true;
- }
- if(is_object($union)) {
- $union = get_object_vars($union);
- }
-
- if(is_string($union) ) {
- $prefix = $this->tablePrefix;
-
- $options = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $union);
- }elseif(is_array($union)){
- if(isset($union[0])) {
- $this->options['union'] = array_merge($this->options['union'],$union);
- return $this;
- }else{
- $options = $union;
- }
- }else{
- E(L('_DATA_TYPE_INVALID_'));
- }
- $this->options['union'][] = $options;
- return $this;
- }
-
- public function cache($key=true,$expire=null,$type=''){
-
- if(is_numeric($key) && is_null($expire)){
- $expire = $key;
- $key = true;
- }
- if(false !== $key)
- $this->options['cache'] = array('key'=>$key,'expire'=>$expire,'type'=>$type);
- return $this;
- }
-
- public function field($field,$except=false){
- if(true === $field) {
- $fields = $this->getDbFields();
- $field = $fields?:'*';
- }elseif($except) {
- if(is_string($field)) {
- $field = explode(',',$field);
- }
- $fields = $this->getDbFields();
- $field = $fields?array_diff($fields,$field):$field;
- }
- $this->options['field'] = $field;
- return $this;
- }
-
- public function scope($scope='',$args=NULL){
- if('' === $scope) {
- if(isset($this->_scope['default'])) {
-
- $options = $this->_scope['default'];
- }else{
- return $this;
- }
- }elseif(is_string($scope)){
- $scopes = explode(',',$scope);
- $options = array();
- foreach ($scopes as $name){
- if(!isset($this->_scope[$name])) continue;
- $options = array_merge($options,$this->_scope[$name]);
- }
- if(!empty($args) && is_array($args)) {
- $options = array_merge($options,$args);
- }
- }elseif(is_array($scope)){
- $options = $scope;
- }
-
- if(is_array($options) && !empty($options)){
- $this->options = array_merge($this->options,array_change_key_case($options));
- }
- return $this;
- }
-
- public function where($where,$parse=null){
- if(!is_null($parse) && is_string($where)) {
- if(!is_array($parse)) {
- $parse = func_get_args();
- array_shift($parse);
- }
- $parse = array_map(array($this->db,'escapeString'),$parse);
- $where = vsprintf($where,$parse);
- }elseif(is_object($where)){
- $where = get_object_vars($where);
- }
- if(is_string($where) && '' != $where){
- $map = array();
- $map['_string'] = $where;
- $where = $map;
- }
- if(isset($this->options['where'])){
- $this->options['where'] = array_merge($this->options['where'],$where);
- }else{
- $this->options['where'] = $where;
- }
-
- return $this;
- }
-
- public function limit($offset,$length=null){
- if(is_null($length) && strpos($offset,',')){
- list($offset,$length) = explode(',',$offset);
- }
- $this->options['limit'] = intval($offset).( $length? ','.intval($length) : '' );
- return $this;
- }
-
- public function page($page,$listRows=null){
- if(is_null($listRows) && strpos($page,',')){
- list($page,$listRows) = explode(',',$page);
- }
- $this->options['page'] = array(intval($page),intval($listRows));
- return $this;
- }
-
- public function comment($comment){
- $this->options['comment'] = $comment;
- return $this;
- }
-
- public function fetchSql($fetch=true){
- $this->options['fetch_sql'] = $fetch;
- return $this;
- }
-
- public function bind($key,$value=false) {
- if(is_array($key)){
- $this->options['bind'] = $key;
- }else{
- $num = func_num_args();
- if($num>2){
- $params = func_get_args();
- array_shift($params);
- $this->options['bind'][$key] = $params;
- }else{
- $this->options['bind'][$key] = $value;
- }
- }
- return $this;
- }
-
- public function setProperty($name,$value) {
- if(property_exists($this,$name))
- $this->$name = $value;
- return $this;
- }
- }
|