| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Ming
- * Date: 2017/10/17
- * Time: 15:52
- */
- namespace common\traits;
- trait ModelTrait{
- /**
- * 提示的信息
- * @var array
- */
- private $_tipMessages = [
- 'data' => null,
- 'message' => [],
- ];
- /**
- * 将所有错误转换成字符窜并用空格隔开,或者只获得其中的一个错误
- *
- * @return string
- */
- public function formatErrors($all = false) {
- $result = '';
- foreach ($this->getErrors() as $errors) {
- if ($all == false) {
- $result = array_pop($errors);
- break;
- }
- $result .= implode("<br />", $errors);
- }
- // if($this->_apiError){
- // $this->_apiError['message'] = $result;
- // return $this->_apiError;
- // }
- return $result;
- }
- /**
- * 添加错误信息
- *
- * @param $error
- * @param $formName
- */
- public function setError($error ,$formName = null) {
- if(!$formName) $formName = $this->formName();
- $this->addError($formName ,$error);
- }
- /**
- * 格式化 sql 中的 in
- * @param array $array
- * @return string
- */
- public function formatIn($array){
- if(!is_array($array)){
- $array = [$array];
- }
- return self::staticFormatIn($array);
- }
- /**
- * 格式化 sql 中的 in
- * @param array $array
- * @return string
- */
- public static function staticFormatIn(array $array){
- $array = array_unique($array);
- $in = "'" . implode("','", $array) . "'";
- return $in;
- }
- /**
- * 设置提示信息
- * @param $message
- * @param null $data
- */
- public function setTipMessage($message, $data = null){
- if(is_array($message)){
- $message = implode("<br />", $message);
- }
- $this->_tipMessages['data'] = $data;
- $this->_tipMessages['message'][] = $message;
- }
- /**
- * 为tip信息重新赋值
- * @param $data
- * @return array
- */
- public function assignTipMessage($data){
- $this->_tipMessages = [
- 'data' => $data['data'],
- 'message' => $data['message'],
- ];
- return $this->_tipMessages;
- }
- /**
- * 获取提示信息
- * @return array
- */
- public function getTipMessage(){
- return $this->_tipMessages;
- }
- /**
- * 是否有提示信息
- * @return bool
- */
- public function hasTipMessage(){
- return (isset($this->_tipMessages['message']) && !empty($this->_tipMessages['message']));
- }
- /**
- * 激活状态
- * @param $value
- * @return bool
- */
- public function isActive($value){
- return (\common\base\ActiveQuery::STATUS_ACTIVED == $value);
- }
- /**
- * 锁定状态
- * @param $value
- * @return bool
- */
- public function isLock($value){
- return (\common\base\ActiveQuery::STATUS_ACTIVED != $value);
- }
- /**
- * 获取抛出的异常信息
- * @param \Exception $e
- * @return string
- */
- public function getThrowMessage(\Exception $e){
- if(defined('YII_DEBUG') && YII_DEBUG === true){
- $message = "错误信息:" . $e->getMessage();
- $message .= " 追踪信息: " . $e->getTraceAsString();
- }else{
- $message = $e->getMessage();
- }
- return $message;
- }
- }
|