MergeModel.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Model;
  12. use Think\Model;
  13. /**
  14. * ThinkPHP 聚合模型扩展
  15. */
  16. class MergeModel extends Model {
  17. protected $modelList = array(); // 包含的模型列表 第一个必须是主表模型
  18. protected $masterModel = ''; // 主模型
  19. protected $joinType = 'INNER'; // 聚合模型的查询JOIN类型
  20. protected $fk = ''; // 外键名 默认为主表名_id
  21. protected $mapFields = array(); // 需要处理的模型映射字段,避免混淆 array( id => 'user.id' )
  22. /**
  23. * 架构函数
  24. * 取得DB类的实例对象 字段检查
  25. * @access public
  26. * @param string $name 模型名称
  27. * @param string $tablePrefix 表前缀
  28. * @param mixed $connection 数据库连接信息
  29. */
  30. public function __construct($name='',$tablePrefix='',$connection=''){
  31. parent::__construct($name,$tablePrefix,$connection);
  32. // 聚合模型的字段信息
  33. if(empty($this->fields) && !empty($this->modelList)){
  34. $fields = array();
  35. foreach($this->modelList as $model){
  36. // 获取模型的字段信息
  37. $result = $this->db->getFields(M($model)->getTableName());
  38. $_fields = array_keys($result);
  39. // $this->mapFields = array_intersect($fields,$_fields);
  40. $fields = array_merge($fields,$_fields);
  41. }
  42. $this->fields = $fields;
  43. }
  44. // 设置第一个模型为主表模型
  45. if(empty($this->masterModel) && !empty($this->modelList)){
  46. $this->masterModel = $this->modelList[0];
  47. }
  48. // 主表的主键名
  49. $this->pk = M($this->masterModel)->getPk();
  50. // 设置默认外键名 仅支持单一外键
  51. if(empty($this->fk)){
  52. $this->fk = strtolower($this->masterModel).'_id';
  53. }
  54. }
  55. /**
  56. * 得到完整的数据表名
  57. * @access public
  58. * @return string
  59. */
  60. public function getTableName() {
  61. if(empty($this->trueTableName)) {
  62. $tableName = array();
  63. $models = $this->modelList;
  64. foreach($models as $model){
  65. $tableName[] = M($model)->getTableName().' '.$model;
  66. }
  67. $this->trueTableName = implode(',',$tableName);
  68. }
  69. return $this->trueTableName;
  70. }
  71. /**
  72. * 自动检测数据表信息
  73. * @access protected
  74. * @return void
  75. */
  76. protected function _checkTableInfo() {}
  77. /**
  78. * 新增聚合数据
  79. * @access public
  80. * @param mixed $data 数据
  81. * @param array $options 表达式
  82. * @param boolean $replace 是否replace
  83. * @return mixed
  84. */
  85. public function add($data='',$options=array(),$replace=false){
  86. if(empty($data)) {
  87. // 没有传递数据,获取当前数据对象的值
  88. if(!empty($this->data)) {
  89. $data = $this->data;
  90. // 重置数据
  91. $this->data = array();
  92. }else{
  93. $this->error = L('_DATA_TYPE_INVALID_');
  94. return false;
  95. }
  96. }
  97. // 启动事务
  98. $this->startTrans();
  99. // 写入主表数据
  100. $result = M($this->masterModel)->strict(false)->add($data);
  101. if($result){
  102. // 写入外键数据
  103. $data[$this->fk] = $result;
  104. $models = $this->modelList;
  105. array_shift($models);
  106. // 写入附表数据
  107. foreach($models as $model){
  108. $res = M($model)->strict(false)->add($data);
  109. if(!$res){
  110. $this->rollback();
  111. return false;
  112. }
  113. }
  114. // 提交事务
  115. $this->commit();
  116. }else{
  117. $this->rollback();
  118. return false;
  119. }
  120. return $result;
  121. }
  122. /**
  123. * 对保存到数据库的数据进行处理
  124. * @access protected
  125. * @param mixed $data 要操作的数据
  126. * @return boolean
  127. */
  128. protected function _facade($data) {
  129. // 检查数据字段合法性
  130. if(!empty($this->fields)) {
  131. if(!empty($this->options['field'])) {
  132. $fields = $this->options['field'];
  133. unset($this->options['field']);
  134. if(is_string($fields)) {
  135. $fields = explode(',',$fields);
  136. }
  137. }else{
  138. $fields = $this->fields;
  139. }
  140. foreach ($data as $key=>$val){
  141. if(!in_array($key,$fields,true)){
  142. unset($data[$key]);
  143. }elseif(array_key_exists($key,$this->mapFields)){
  144. // 需要处理映射字段
  145. $data[$this->mapFields[$key]] = $val;
  146. unset($data[$key]);
  147. }
  148. }
  149. }
  150. // 安全过滤
  151. if(!empty($this->options['filter'])) {
  152. $data = array_map($this->options['filter'],$data);
  153. unset($this->options['filter']);
  154. }
  155. $this->_before_write($data);
  156. return $data;
  157. }
  158. /**
  159. * 保存聚合模型数据
  160. * @access public
  161. * @param mixed $data 数据
  162. * @param array $options 表达式
  163. * @return boolean
  164. */
  165. public function save($data='',$options=array()){
  166. // 根据主表的主键更新
  167. if(empty($data)) {
  168. // 没有传递数据,获取当前数据对象的值
  169. if(!empty($this->data)) {
  170. $data = $this->data;
  171. // 重置数据
  172. $this->data = array();
  173. }else{
  174. $this->error = L('_DATA_TYPE_INVALID_');
  175. return false;
  176. }
  177. }
  178. if(empty($data)){
  179. // 没有数据则不执行
  180. $this->error = L('_DATA_TYPE_INVALID_');
  181. return false;
  182. }
  183. // 如果存在主键数据 则自动作为更新条件
  184. $pk = $this->pk;
  185. if(isset($data[$pk])) {
  186. $where[$pk] = $data[$pk];
  187. $options['where'] = $where;
  188. unset($data[$pk]);
  189. }
  190. $options['join'] = '';
  191. $options = $this->_parseOptions($options);
  192. // 更新操作不使用JOIN
  193. $options['table'] = $this->getTableName();
  194. if(is_array($options['where']) && isset($options['where'][$pk])){
  195. $pkValue = $options['where'][$pk];
  196. }
  197. if(false === $this->_before_update($data,$options)) {
  198. return false;
  199. }
  200. $result = $this->db->update($data,$options);
  201. if(false !== $result) {
  202. if(isset($pkValue)) $data[$pk] = $pkValue;
  203. $this->_after_update($data,$options);
  204. }
  205. return $result;
  206. }
  207. /**
  208. * 删除聚合模型数据
  209. * @access public
  210. * @param mixed $options 表达式
  211. * @return mixed
  212. */
  213. public function delete($options=array()){
  214. $pk = $this->pk;
  215. if(empty($options) && empty($this->options['where'])) {
  216. // 如果删除条件为空 则删除当前数据对象所对应的记录
  217. if(!empty($this->data) && isset($this->data[$pk]))
  218. return $this->delete($this->data[$pk]);
  219. else
  220. return false;
  221. }
  222. if(is_numeric($options) || is_string($options)) {
  223. // 根据主键删除记录
  224. if(strpos($options,',')) {
  225. $where[$pk] = array('IN', $options);
  226. }else{
  227. $where[$pk] = $options;
  228. }
  229. $options = array();
  230. $options['where'] = $where;
  231. }
  232. // 分析表达式
  233. $options['join'] = '';
  234. $options = $this->_parseOptions($options);
  235. if(empty($options['where'])){
  236. // 如果条件为空 不进行删除操作 除非设置 1=1
  237. return false;
  238. }
  239. if(is_array($options['where']) && isset($options['where'][$pk])){
  240. $pkValue = $options['where'][$pk];
  241. }
  242. $options['table'] = implode(',',$this->modelList);
  243. $options['using'] = $this->getTableName();
  244. if(false === $this->_before_delete($options)) {
  245. return false;
  246. }
  247. $result = $this->db->delete($options);
  248. if(false !== $result) {
  249. $data = array();
  250. if(isset($pkValue)) $data[$pk] = $pkValue;
  251. $this->_after_delete($data,$options);
  252. }
  253. // 返回删除记录个数
  254. return $result;
  255. }
  256. /**
  257. * 表达式过滤方法
  258. * @access protected
  259. * @param string $options 表达式
  260. * @return void
  261. */
  262. protected function _options_filter(&$options) {
  263. if(!isset($options['join'])){
  264. $models = $this->modelList;
  265. array_shift($models);
  266. foreach($models as $model){
  267. $options['join'][] = $this->joinType.' JOIN '.M($model)->getTableName().' '.$model.' ON '.$this->masterModel.'.'.$this->pk.' = '.$model.'.'.$this->fk;
  268. }
  269. }
  270. $options['table'] = M($this->masterModel)->getTableName().' '.$this->masterModel;
  271. $options['field'] = $this->checkFields(isset($options['field'])?$options['field']:'');
  272. if(isset($options['group']))
  273. $options['group'] = $this->checkGroup($options['group']);
  274. if(isset($options['where']))
  275. $options['where'] = $this->checkCondition($options['where']);
  276. if(isset($options['order']))
  277. $options['order'] = $this->checkOrder($options['order']);
  278. }
  279. /**
  280. * 检查条件中的聚合字段
  281. * @access protected
  282. * @param mixed $data 条件表达式
  283. * @return array
  284. */
  285. protected function checkCondition($where) {
  286. if(is_array($where)) {
  287. $view = array();
  288. foreach($where as $name=>$value){
  289. if(array_key_exists($name,$this->mapFields)){
  290. // 需要处理映射字段
  291. $view[$this->mapFields[$name]] = $value;
  292. unset($where[$name]);
  293. }
  294. }
  295. $where = array_merge($where,$view);
  296. }
  297. return $where;
  298. }
  299. /**
  300. * 检查Order表达式中的聚合字段
  301. * @access protected
  302. * @param string $order 字段
  303. * @return string
  304. */
  305. protected function checkOrder($order='') {
  306. if(is_string($order) && !empty($order)) {
  307. $orders = explode(',',$order);
  308. $_order = array();
  309. foreach ($orders as $order){
  310. $array = explode(' ',trim($order));
  311. $field = $array[0];
  312. $sort = isset($array[1])?$array[1]:'ASC';
  313. if(array_key_exists($field,$this->mapFields)){
  314. // 需要处理映射字段
  315. $field = $this->mapFields[$field];
  316. }
  317. $_order[] = $field.' '.$sort;
  318. }
  319. $order = implode(',',$_order);
  320. }
  321. return $order;
  322. }
  323. /**
  324. * 检查Group表达式中的聚合字段
  325. * @access protected
  326. * @param string $group 字段
  327. * @return string
  328. */
  329. protected function checkGroup($group='') {
  330. if(!empty($group)) {
  331. $groups = explode(',',$group);
  332. $_group = array();
  333. foreach ($groups as $field){
  334. // 解析成聚合字段
  335. if(array_key_exists($field,$this->mapFields)){
  336. // 需要处理映射字段
  337. $field = $this->mapFields[$field];
  338. }
  339. $_group[] = $field;
  340. }
  341. $group = implode(',',$_group);
  342. }
  343. return $group;
  344. }
  345. /**
  346. * 检查fields表达式中的聚合字段
  347. * @access protected
  348. * @param string $fields 字段
  349. * @return string
  350. */
  351. protected function checkFields($fields='') {
  352. if(empty($fields) || '*'==$fields ) {
  353. // 获取全部聚合字段
  354. $fields = $this->fields;
  355. }
  356. if(!is_array($fields))
  357. $fields = explode(',',$fields);
  358. // 解析成聚合字段
  359. $array = array();
  360. foreach ($fields as $field){
  361. if(array_key_exists($field,$this->mapFields)){
  362. // 需要处理映射字段
  363. $array[] = $this->mapFields[$field].' AS '.$field;
  364. }else{
  365. $array[] = $field;
  366. }
  367. }
  368. $fields = implode(',',$array);
  369. return $fields;
  370. }
  371. /**
  372. * 获取数据表字段信息
  373. * @access public
  374. * @return array
  375. */
  376. public function getDbFields(){
  377. return $this->fields;
  378. }
  379. }