CommonModel.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class CommonModel extends Model {
  10. /**
  11. +----------------------------------------------------------
  12. * 把返回的数据集转换成Tree
  13. +----------------------------------------------------------
  14. * @access public
  15. +----------------------------------------------------------
  16. * @param array $list 要转换的数据集
  17. * @param string $pid parent标记字段
  18. * @param string $level level标记字段
  19. +----------------------------------------------------------
  20. * @return array
  21. +----------------------------------------------------------
  22. */
  23. public function toTree($list=null, $pk='id',$pid = 'pid',$child = '_child')
  24. {
  25. if(null === $list) {
  26. // 默认直接取查询返回的结果集合
  27. $list = &$this->dataList;
  28. }
  29. // 创建Tree
  30. $tree = array();
  31. if(is_array($list)) {
  32. // 创建基于主键的数组引用
  33. $refer = array();
  34. foreach ($list as $key => $data) {
  35. $_key = is_object($data)?$data->$pk:$data[$pk];
  36. $refer[$_key] =& $list[$key];
  37. }
  38. foreach ($list as $key => $data) {
  39. // 判断是否存在parent
  40. $parentId = is_object($data)?$data->$pid:$data[$pid];
  41. $is_exist_pid = false;
  42. foreach($refer as $k=>$v)
  43. {
  44. if($parentId==$k)
  45. {
  46. $is_exist_pid = true;
  47. break;
  48. }
  49. }
  50. if ($is_exist_pid) {
  51. if (isset($refer[$parentId])) {
  52. $parent =& $refer[$parentId];
  53. $parent[$child][] =& $list[$key];
  54. }
  55. } else {
  56. $tree[] =& $list[$key];
  57. }
  58. }
  59. }
  60. return $tree;
  61. }
  62. /**
  63. * 将格式数组转换为树
  64. *
  65. * @param array $list
  66. * @param integer $level 进行递归时传递用的参数
  67. */
  68. private $formatTree; //用于树型数组完成递归格式的全局变量
  69. private function _toFormatTree($list,$level=0,$title = 'title')
  70. {
  71. foreach($list as $key=>$val)
  72. {
  73. $tmp_str=str_repeat("&nbsp;&nbsp;",$level*2);
  74. $tmp_str.="|--";
  75. $val['level'] = $level;
  76. $val['title_show'] = $tmp_str.$val[$title];
  77. if(!array_key_exists('_child',$val))
  78. {
  79. array_push($this->formatTree,$val);
  80. }
  81. else
  82. {
  83. $tmp_ary = $val['_child'];
  84. unset($val['_child']);
  85. array_push($this->formatTree,$val);
  86. $this->_toFormatTree($tmp_ary,$level+1,$title); //进行下一层递归
  87. }
  88. }
  89. return;
  90. }
  91. public function toFormatTree($list,$title = 'title')
  92. {
  93. $list = $this->toTree($list);
  94. $this->formatTree = array();
  95. $this->_toFormatTree($list,0,$title);
  96. return $this->formatTree;
  97. }
  98. public function toNameFormatTree($list,$title = 'name')
  99. {
  100. $list = $this->toTree($list);
  101. $this->formatTree = array();
  102. $this->_toFormatTree($list,0,$title);
  103. return $this->formatTree;
  104. }
  105. //无限递归获取子数据ID集合
  106. private $childIds;
  107. private function _getChildIds($pid = '0', $pk_str='id' , $pid_str ='pid')
  108. {
  109. $childItem_arr = $this->field('id')->where($pid_str."=".$pid)->findAll();
  110. if($childItem_arr)
  111. {
  112. foreach($childItem_arr as $childItem)
  113. {
  114. $this->childIds[] = $childItem[$pk_str];
  115. $this->_getChildIds($childItem[$pk_str],$pk_str,$pid_str);
  116. }
  117. }
  118. }
  119. public function getChildIds($pid = '0', $pk_str='id' , $pid_str ='pid')
  120. {
  121. $this->childIds = array();
  122. $this->_getChildIds($pid,$pk_str,$pid_str);
  123. return $this->childIds;
  124. }
  125. }
  126. ?>