xhBaseModel.php 4.4 KB

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