ModelTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Ming
  5. * Date: 2017/10/17
  6. * Time: 15:52
  7. */
  8. namespace common\traits;
  9. trait ModelTrait{
  10. /**
  11. * 提示的信息
  12. * @var array
  13. */
  14. private $_tipMessages = [
  15. 'data' => null,
  16. 'message' => [],
  17. ];
  18. /**
  19. * 将所有错误转换成字符窜并用空格隔开,或者只获得其中的一个错误
  20. *
  21. * @return string
  22. */
  23. public function formatErrors($all = false) {
  24. $result = '';
  25. foreach ($this->getErrors() as $errors) {
  26. if ($all == false) {
  27. $result = array_pop($errors);
  28. break;
  29. }
  30. $result .= implode("<br />", $errors);
  31. }
  32. // if($this->_apiError){
  33. // $this->_apiError['message'] = $result;
  34. // return $this->_apiError;
  35. // }
  36. return $result;
  37. }
  38. /**
  39. * 添加错误信息
  40. *
  41. * @param $error
  42. * @param $formName
  43. */
  44. public function setError($error ,$formName = null) {
  45. if(!$formName) $formName = $this->formName();
  46. $this->addError($formName ,$error);
  47. }
  48. /**
  49. * 格式化 sql 中的 in
  50. * @param array $array
  51. * @return string
  52. */
  53. public function formatIn($array){
  54. if(!is_array($array)){
  55. $array = [$array];
  56. }
  57. return self::staticFormatIn($array);
  58. }
  59. /**
  60. * 格式化 sql 中的 in
  61. * @param array $array
  62. * @return string
  63. */
  64. public static function staticFormatIn(array $array){
  65. $array = array_unique($array);
  66. $in = "'" . implode("','", $array) . "'";
  67. return $in;
  68. }
  69. /**
  70. * 设置提示信息
  71. * @param $message
  72. * @param null $data
  73. */
  74. public function setTipMessage($message, $data = null){
  75. if(is_array($message)){
  76. $message = implode("<br />", $message);
  77. }
  78. $this->_tipMessages['data'] = $data;
  79. $this->_tipMessages['message'][] = $message;
  80. }
  81. /**
  82. * 为tip信息重新赋值
  83. * @param $data
  84. * @return array
  85. */
  86. public function assignTipMessage($data){
  87. $this->_tipMessages = [
  88. 'data' => $data['data'],
  89. 'message' => $data['message'],
  90. ];
  91. return $this->_tipMessages;
  92. }
  93. /**
  94. * 获取提示信息
  95. * @return array
  96. */
  97. public function getTipMessage(){
  98. return $this->_tipMessages;
  99. }
  100. /**
  101. * 是否有提示信息
  102. * @return bool
  103. */
  104. public function hasTipMessage(){
  105. return (isset($this->_tipMessages['message']) && !empty($this->_tipMessages['message']));
  106. }
  107. /**
  108. * 激活状态
  109. * @param $value
  110. * @return bool
  111. */
  112. public function isActive($value){
  113. return (\common\base\ActiveQuery::STATUS_ACTIVED == $value);
  114. }
  115. /**
  116. * 锁定状态
  117. * @param $value
  118. * @return bool
  119. */
  120. public function isLock($value){
  121. return (\common\base\ActiveQuery::STATUS_ACTIVED != $value);
  122. }
  123. /**
  124. * 获取抛出的异常信息
  125. * @param \Exception $e
  126. * @return string
  127. */
  128. public function getThrowMessage(\Exception $e){
  129. if(defined('YII_DEBUG') && YII_DEBUG === true){
  130. $message = "错误信息:" . $e->getMessage();
  131. $message .= " 追踪信息: " . $e->getTraceAsString();
  132. }else{
  133. $message = $e->getMessage();
  134. }
  135. return $message;
  136. }
  137. }