xhBaseModel.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\db\ActiveRecord;
  5. class xhBaseModel extends ActiveRecord
  6. {
  7. /**
  8. * 添加
  9. * @param $data
  10. * @param bool $json 是否使用json返回
  11. * @return bool
  12. */
  13. public static function add($data, $json = false)
  14. {
  15. $self = get_called_class();
  16. $model = new $self;
  17. $attributes = $model->attributes;
  18. $fields = array_keys($attributes);
  19. foreach($fields as $key){
  20. if(isset($data[$key])){
  21. $model->$key = $data[$key];
  22. }
  23. }
  24. $model->save();
  25. $e = $model->getErrors();
  26. if(!empty($e)){
  27. var_dump($e);
  28. Yii::$app->end();
  29. }
  30. return $model->attributes;
  31. }
  32. //批量添加
  33. public static function batchAdd($data)
  34. {
  35. $self = get_called_class();
  36. $model = new $self;
  37. foreach($data as $attributes)
  38. {
  39. $_model = clone $model;
  40. $_model->attributes = $attributes;
  41. $_model->save();
  42. }
  43. }
  44. /**
  45. * 取表字段
  46. */
  47. public static function getFields()
  48. {
  49. $self = get_called_class();
  50. $model = new $self;
  51. $attributes = $model->attributes;
  52. return array_keys($attributes);
  53. }
  54. /**
  55. * 根据主键ID删除一条记录
  56. */
  57. public static function deleteById($id)
  58. {
  59. $return = self::findOne($id)->delete();
  60. return $return;
  61. }
  62. /**
  63. *根据条件删除一条或多条记录
  64. */
  65. public static function deleteByCondition($condition)
  66. {
  67. if(empty($condition))
  68. return 0;
  69. $where = array();
  70. $variable = array();
  71. foreach($condition as $key => $val){
  72. $where[] = $key.'=:'.$key;
  73. $variable[':'.$key] = $val;
  74. }
  75. $whereString = implode(' and ',$where);
  76. $count = self::deleteAll($whereString,$variable);
  77. return $count;
  78. }
  79. /**
  80. * 根据主键ID更新一条记录
  81. * @param $id
  82. * @param $data
  83. * @return array 更新成功返回status:true,失败返回status:false;更新的字段在data内(非表字段会被删除)
  84. */
  85. public static function updateById($id, $data)
  86. {
  87. $self = get_called_class();
  88. $model = new $self;
  89. $attributes = $model->attributes;
  90. $fields = array_keys($attributes);
  91. foreach($data as $key => $val){
  92. if(in_array($key, $fields) == false){
  93. unset($data[$key]);//删除不需要保存的字段
  94. }
  95. }
  96. $re = self::updateAll($data,'id=:id',['id' => $id]);
  97. if($re == 1){
  98. return ['status'=>true, 'data'=>$data];
  99. }
  100. return ['status'=>false, 'data'=>$data];
  101. }
  102. /**
  103. * 根据条件更新一条或多条记录
  104. */
  105. public static function updateByCondition($condition,$data)
  106. {
  107. if(empty($condition) || !is_array($condition)){
  108. throw new \Exception("xhBaseModel updateByCondition action's parameter 'condition' is empty or not a array!");
  109. }
  110. $where = array();
  111. $variable = array();
  112. foreach($condition as $key => $val){
  113. $where[] = $key.'=:'.$key;
  114. $variable[':'.$key] = $val;
  115. }
  116. $whereString = implode(' and ',$where);
  117. $count = self::updateAll($data,$whereString,$variable);
  118. return $count;
  119. }
  120. /**
  121. * 根据主键ID查出一条记录
  122. */
  123. public static function getById($id)
  124. {
  125. return self::find()->where(['id' => $id])->asArray()->one();
  126. }
  127. /**
  128. *根据条件查出一条记录
  129. */
  130. public static function getByCondition($condition)
  131. {
  132. return self::find()->where($condition)->asArray()->one();
  133. }
  134. /**
  135. *根据条件查出多条记录
  136. *
  137. *$instant 即时加载的对象名
  138. *
  139. */
  140. public static function getAllByCondition($condition, $order = null, $field = '*')
  141. {
  142. $query = self::find()->select($field)->where($condition);
  143. if(isset($order)){
  144. $query->orderBy($order);
  145. }
  146. return $query->asArray()->all();
  147. }
  148. /**
  149. * 根据多个主键ID查询多条记录
  150. */
  151. public static function getByIds($ids,$order=null)
  152. {
  153. $data = [];
  154. if(!empty($ids)){
  155. $query = self::find()->where(['id'=>$ids]);
  156. if(!empty($order)){
  157. $query->order($order);
  158. }
  159. $data = $query->asArray()->all();
  160. }
  161. return $data;
  162. }
  163. /**
  164. *根据条件查出 总数
  165. */
  166. public static function getCount($condition){
  167. return self::find()->where($condition)->count();
  168. }
  169. }