MongoAr.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * mongodb模型自定义基类
  4. */
  5. class MongoAr extends EMongoDocument
  6. {
  7. protected $_rmongoDb;
  8. protected $_scenario_error = ''; //数据库操作的错误信息
  9. protected $_action_info = ''; //数据库操作的提示信息
  10. protected $_need_log = true; //是否需要记录
  11. public $_need_ruser_log = false; //是否需要记录前台用户操作
  12. protected $_action_log = ''; //操作记录
  13. public static $c_time = 0;
  14. public function __construct($scenario='insert'){
  15. parent::__construct($scenario);
  16. self::$c_time = time();
  17. $this->onAfterSave = function($event){
  18. $model = $event->sender;
  19. if (Yii::app()->params['app'] == 'console' || !$model->getNeedLog()){
  20. return false;
  21. }
  22. $action_user = Yii::app()->user->getId();
  23. $user_type = '';
  24. if (!$action_user){
  25. $ruser = Yii::app()->request->getParam('user_id','');
  26. if(!empty($ruser) && $this->_need_ruser_log){
  27. $ruser = CommonFn::getObj($ruser,'ZUser');
  28. if($ruser){
  29. $action_user = $ruser->_id;
  30. $user_type = 'ruser';
  31. }else{
  32. return false;
  33. }
  34. }else{
  35. return false;
  36. }
  37. }
  38. $db_name = $model->getMongoDBComponent()->dbName;
  39. $c_name = $model->getCollectionName();
  40. $scenario = $model->getScenario();
  41. $_id = $model->_id;
  42. //echo($_id);exit;
  43. $action_log = $model->getActionLog();
  44. $result = DbAction::model()->getCollection()->findAndModify(
  45. array('db_name' => $db_name, 'c_name' => $c_name, 'r_id' => $_id),
  46. array('$push' => array('action' => array('user' => $action_user,'user_type'=>$user_type ,'time' => time(), 'scenario' => $scenario, 'action_log' => $action_log))),
  47. null,
  48. array('new' => true, 'upsert' => true)
  49. );
  50. };
  51. }
  52. public function setActionLog($log){
  53. $this->_action_log = $log;
  54. }
  55. public function getActionLog(){
  56. return $this->_action_log;
  57. }
  58. //格式化输出
  59. public function parse($obj,$muti=true,$outputArr=array()){
  60. if($muti){
  61. $newRows = array();
  62. foreach ($obj as $k => $v){
  63. $newRows[$k] = $this->parseRow($v,$outputArr);
  64. }
  65. return $newRows;
  66. }else{
  67. return $this->parseRow($obj,$outputArr);
  68. }
  69. }
  70. public function parseRow($row,$outputArr=array()){
  71. return $this->output($row,$outputArr);
  72. }
  73. public function output($row,$outputArr = array()){
  74. $newRow = array();
  75. if(is_array($outputArr) && !empty($outputArr)){
  76. foreach($row as $k=>$v){
  77. if(in_array($k,$outputArr)){
  78. $newRow[$k] = $v;
  79. }
  80. }
  81. }else{
  82. $newRow = $row;
  83. }
  84. return $newRow;
  85. }
  86. public function getCollectionName(){
  87. return '';
  88. }
  89. /**
  90. * 获取自增的id
  91. */
  92. public function get_new_id(){
  93. $collection = $this->getCollectionName();
  94. if ($collection == ''){
  95. return 0;
  96. }
  97. $cursor = AutoIncrement::model()->getCollection()->findAndModify(array('_id' => $collection), array('$inc' => array('currentIdValue' => 1)), array('currentIdValue' => 1), array('new' => true, 'upsert' => true));
  98. $_id = $cursor['currentIdValue'];
  99. return $_id;
  100. }
  101. /**
  102. * 获取自增的id
  103. */
  104. public function getNewId($step=1){
  105. $collection = $this->getCollectionName();
  106. if ($collection == ''){
  107. return 0;
  108. }
  109. $ai_model = AutoIncrement::model();
  110. $ai_model->setMongoDBComponent($this->getMongoDBComponent());
  111. $cursor = $ai_model->getCollection()->findAndModify(array('_id' => $collection), array('$inc' => array('currentIdValue' => $step)), array('currentIdValue' => 1), array('new' => true, 'upsert' => true));
  112. $_id = $cursor['currentIdValue'];
  113. return $_id;
  114. }
  115. /**
  116. * 重写获取数据库组件函数,使每个模型的数据库独立
  117. */
  118. public function getMongoDBComponent()
  119. {
  120. if($this->_rmongoDb===null)
  121. $this->_rmongoDb = Yii::app()->getComponent('mongodb');
  122. return $this->_rmongoDb;
  123. }
  124. /**
  125. * 重写设置数据库组件函数,使每个模型的数据库独立
  126. */
  127. public function setMongoDBComponent(EMongoDB $component)
  128. {
  129. $this->_rmongoDb = $component;
  130. }
  131. /**
  132. * 设置错误信息
  133. */
  134. public function setScenarioError($message){
  135. $this->_scenario_error = $message;
  136. }
  137. /**
  138. * 获取错误信息
  139. */
  140. public function getScenarioError(){
  141. return $this->_scenario_error;
  142. }
  143. /**
  144. * 设置提示信息
  145. */
  146. public function setActionInfo($message){
  147. $this->_action_info = $message;
  148. }
  149. /**
  150. * 获取提示信息
  151. */
  152. public function getActionInfo(){
  153. return $this->_action_info;
  154. }
  155. /**
  156. * 是否需要操作记录
  157. */
  158. public function getNeedLog(){
  159. return $this->_need_log;
  160. }
  161. }