Model.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. *****************************************************************
  4. * 联系QQ: 290802026 *
  5. * 版 本: V2.0 *
  6. * 开发者:雨中磐石工作室 *
  7. * 邮 箱: admin@rockoa.com *
  8. * 网 址: http://www.rockoa.com/ *
  9. * 说 明: 数据模型 *
  10. * 备 注: 未经允许不得商业出售,代码欢迎参考纠正 *
  11. *****************************************************************
  12. */
  13. abstract class Model{
  14. public $perfix = PREFIX;
  15. public $rock;
  16. public $db;
  17. public $table;
  18. public $adminname;
  19. public $adminid;
  20. public $tempxinxi = array();
  21. public function __construct($table='')
  22. {
  23. $this->rock = $GLOBALS['rock'];
  24. $this->db = $GLOBALS['db'];
  25. $this->adminid = $this->rock->adminid;
  26. $this->adminname = $this->rock->adminname;
  27. $this->settable($table);
  28. $this->initModel();
  29. }
  30. public function settable($table, $qzbo=true)
  31. {
  32. $this->table = ''.$this->perfix.''.$table.'';
  33. if(!$qzbo)$this->table = $table;
  34. }
  35. public function initModel(){}
  36. public function getmou($fields, $where, $order='')
  37. {
  38. return $this->db->getmou($this->table, $fields, $where, $order);
  39. }
  40. public function getone($where, $fields='*', $order='')
  41. {
  42. return $this->db->getone($this->table, $where, $fields, $order);
  43. }
  44. public function getrows($where, $fields='*', $order='', $limit='')
  45. {
  46. return $this->db->getrows($this->table, $where, $fields, $order, $limit);
  47. }
  48. public function getall($where, $fields='*', $order='', $limit='')
  49. {
  50. $sql = $this->db->getsql(array(
  51. 'fields' => $fields,
  52. 'table' => $this->table,
  53. 'where' => $where,
  54. 'order' => $order,
  55. 'limit' => $limit
  56. ));
  57. return $this->db->getall($sql);
  58. }
  59. public function getarr($where, $fields='*', $kfied='id')
  60. {
  61. return $this->db->getarr($this->table, $where, $fields, $kfied);
  62. }
  63. public function rows($where)
  64. {
  65. return $this->db->rows($this->table, $where);
  66. }
  67. public function query($where, $fields='*', $order='', $limit='')
  68. {
  69. $sql = $this->db->getsql(array(
  70. 'fields' => $fields,
  71. 'table' => $this->table,
  72. 'where' => $where,
  73. 'order' => $order,
  74. 'limit' => $limit
  75. ));
  76. return $this->db->query($sql);
  77. }
  78. public function record($arr, $where='')
  79. {
  80. return $this->db->record($this->table, $arr, $where);
  81. }
  82. public function update($arr,$where)
  83. {
  84. return $this->record($arr, $where);
  85. }
  86. public function insert($arr)
  87. {
  88. $nid = 0;
  89. if($this->record($arr, ''))$nid = $this->db->insert_id();
  90. return $nid;
  91. }
  92. public function insertAll($arr)
  93. {
  94. $name = $values = '';
  95. foreach($arr as $k=>$rs){
  96. $cont = '';
  97. foreach($rs as $i=>$v){
  98. if($k==0)$name.=',`'.$i.'`';
  99. $cont.=",".$this->db->toaddval($v)."";
  100. }
  101. $cont = substr($cont, 1);
  102. if($k>0)$values.=',';
  103. $values.='('.$cont.')';
  104. }
  105. return $this->db->insert($this->table, substr($name, 1),'values '.$values.'', true);
  106. }
  107. public function getwhere($where='')
  108. {
  109. return $this->db->getwhere($where);
  110. }
  111. public function getfields()
  112. {
  113. return $this->db->getallfields($this->table);
  114. }
  115. public function delete($where)
  116. {
  117. return $this->db->delete($this->table, $where);
  118. }
  119. public function getlimit($where, $page=1, $fields='*', $order='', $limit=20, $table='')
  120. {
  121. if($order != '')$order = 'order by '.$order.'';
  122. $where = $this->getwhere($where);
  123. if($table == '')$table = $this->table;
  124. $sql = "select $fields from $table where $where $order ";
  125. $count = $this->db->rows($table, $where);
  126. if($page <= 0)$page=1;
  127. $sql .= "limit ".($page-1)*$limit.",$limit";
  128. $rows = $this->db->getall($sql);
  129. $maxpage = ceil($count/$limit);
  130. return array(
  131. 'rows' => $rows,
  132. 'count' => $count,
  133. 'maxpage' => $maxpage,
  134. 'page' => $page,
  135. 'limit' => $limit,
  136. 'prevpage' => $page-1,
  137. 'nextpage' => $page+1,
  138. 'url' => ''
  139. );
  140. }
  141. public function isempt($str)
  142. {
  143. return $this->rock->isempt($str);
  144. }
  145. public function contain($str, $s1)
  146. {
  147. return $this->rock->contain($str, $s1);
  148. }
  149. public function getLastSql()
  150. {
  151. return $this->db->getLastSql();
  152. }
  153. public function count($where='1=1')
  154. {
  155. return $this->rows($where);
  156. }
  157. public function getXinxi($id,$fields='*')
  158. {
  159. if(isset($this->tempxinxi[$id]))return $this->tempxinxi[$id];
  160. $rs = $this->getone($id,$fields);
  161. $this->tempxinxi[$id] = $rs;
  162. return $rs;
  163. }
  164. }
  165. class sModel extends Model{}
粤ICP备19079148号