Model.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 function __construct($table='')
  21. {
  22. $this->rock = $GLOBALS['rock'];
  23. $this->db = $GLOBALS['db'];
  24. $this->adminid = $this->rock->adminid;
  25. $this->adminname = $this->rock->adminname;
  26. $this->settable($table);
  27. $this->initModel();
  28. }
  29. public function settable($table, $qzbo=true)
  30. {
  31. $this->table = ''.$this->perfix.''.$table.'';
  32. if(!$qzbo)$this->table = $table;
  33. }
  34. public function initModel(){}
  35. public function getmou($fields, $where, $order='')
  36. {
  37. return $this->db->getmou($this->table, $fields, $where, $order);
  38. }
  39. public function getone($where, $fields='*', $order='')
  40. {
  41. return $this->db->getone($this->table, $where, $fields, $order);
  42. }
  43. public function getrows($where, $fields='*', $order='', $limit='')
  44. {
  45. return $this->db->getrows($this->table, $where, $fields, $order, $limit);
  46. }
  47. public function getall($where, $fields='*', $order='', $limit='')
  48. {
  49. $sql = $this->db->getsql(array(
  50. 'fields' => $fields,
  51. 'table' => $this->table,
  52. 'where' => $where,
  53. 'order' => $order,
  54. 'limit' => $limit
  55. ));
  56. return $this->db->getall($sql);
  57. }
  58. public function getarr($where, $fields='*', $kfied='id')
  59. {
  60. return $this->db->getarr($this->table, $where, $fields, $kfied);
  61. }
  62. public function rows($where)
  63. {
  64. return $this->db->rows($this->table, $where);
  65. }
  66. public function query($where, $fields='*', $order='', $limit='')
  67. {
  68. $sql = $this->db->getsql(array(
  69. 'fields' => $fields,
  70. 'table' => $this->table,
  71. 'where' => $where,
  72. 'order' => $order,
  73. 'limit' => $limit
  74. ));
  75. return $this->db->query($sql);
  76. }
  77. public function record($arr, $where='')
  78. {
  79. return $this->db->record($this->table, $arr, $where);
  80. }
  81. public function update($arr,$where)
  82. {
  83. return $this->record($arr, $where);
  84. }
  85. public function insert($arr)
  86. {
  87. $nid = 0;
  88. if($this->record($arr, ''))$nid = $this->db->insert_id();
  89. return $nid;
  90. }
  91. public function insertAll($arr)
  92. {
  93. $name = $values = '';
  94. foreach($arr as $k=>$rs){
  95. $cont = '';
  96. foreach($rs as $i=>$v){
  97. if($k==0)$name.=',`'.$i.'`';
  98. $cont.=",".$this->db->toaddval($v)."";
  99. }
  100. $cont = substr($cont, 1);
  101. if($k>0)$values.=',';
  102. $values.='('.$cont.')';
  103. }
  104. return $this->db->insert($this->table, substr($name, 1),'values '.$values.'', true);
  105. }
  106. public function getwhere($where='')
  107. {
  108. return $this->db->getwhere($where);
  109. }
  110. public function getfields()
  111. {
  112. return $this->db->getallfields($this->table);
  113. }
  114. public function delete($where)
  115. {
  116. return $this->db->delete($this->table, $where);
  117. }
  118. public function getlimit($where, $page=1, $fields='*', $order='', $limit=20, $table='')
  119. {
  120. if($order != '')$order = 'order by '.$order.'';
  121. $where = $this->getwhere($where);
  122. if($table == '')$table = $this->table;
  123. $sql = "select $fields from $table where $where $order ";
  124. $count = $this->db->rows($table, $where);
  125. if($page <= 0)$page=1;
  126. $sql .= "limit ".($page-1)*$limit.",$limit";
  127. $rows = $this->db->getall($sql);
  128. $maxpage = ceil($count/$limit);
  129. return array(
  130. 'rows' => $rows,
  131. 'count' => $count,
  132. 'maxpage' => $maxpage,
  133. 'page' => $page,
  134. 'limit' => $limit,
  135. 'prevpage' => $page-1,
  136. 'nextpage' => $page+1,
  137. 'url' => ''
  138. );
  139. }
  140. public function isempt($str)
  141. {
  142. return $this->rock->isempt($str);
  143. }
  144. public function contain($str, $s1)
  145. {
  146. return $this->rock->contain($str, $s1);
  147. }
  148. public function getLastSql()
  149. {
  150. return $this->db->getLastSql();
  151. }
  152. public function count($where='1=1')
  153. {
  154. return $this->rows($where);
  155. }
  156. }
  157. class sModel extends Model{}