DbOperation.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\http;
  3. use think\Db;
  4. trait DbOperation
  5. {
  6. /**
  7. * 存储
  8. *
  9. * @author 许祖兴 < zuxing.xu@lettered.cn>
  10. * @date 2020/3/16 10:52
  11. *
  12. * @param array $data
  13. * @return bool
  14. */
  15. public function storeBy(array $data)
  16. {
  17. if ($this->allowField($this->field)->save($data)) {
  18. return $this->{$this->getPk()};
  19. }
  20. return false;
  21. }
  22. /**
  23. * 更新
  24. *
  25. * @author 许祖兴 < zuxing.xu@lettered.cn>
  26. * @date 2020/3/16 10:52
  27. *
  28. * @param $id
  29. * @param $data
  30. * @param string $field
  31. * @return bool
  32. */
  33. public function updateBy($id, $data, $field = ''): bool
  34. {
  35. if (static::update($data, [$field ? : $this->getPk() => $id], $this->field)) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. /**
  41. *
  42. * @author 许祖兴 < zuxing.xu@lettered.cn>
  43. * @date 2020/3/16 10:52
  44. *
  45. * @param $id
  46. * @param array $field
  47. * @param bool $trash
  48. * @return mixed
  49. */
  50. public function findBy($id, array $field = ['*'], $trash = false)
  51. {
  52. if ($trash) {
  53. return static::onlyTrashed()->find($id);
  54. }
  55. return static::where($this->getPk(), $id)->field($field)->find();
  56. }
  57. /**
  58. *
  59. * @author 许祖兴 < zuxing.xu@lettered.cn>
  60. * @date 2020/3/16 10:52
  61. *
  62. * @param $id
  63. * @param bool $force
  64. * @return mixed
  65. */
  66. public function deleteBy($id, $force = false)
  67. {
  68. return static::destroy($id, $force);
  69. }
  70. /**
  71. *
  72. * @author 许祖兴 < zuxing.xu@lettered.cn>
  73. * @date 2020/3/16 10:52
  74. *
  75. * @param $id
  76. * @return mixed
  77. */
  78. public function recover($id)
  79. {
  80. return static::onlyTrashed()->find($id)->restore();
  81. }
  82. /**
  83. *
  84. * @author 许祖兴 < zuxing.xu@lettered.cn>
  85. * @date 2020/3/16 10:52
  86. *
  87. * @return mixed
  88. */
  89. public function getDeleteAtField()
  90. {
  91. return $this->deleteTime;
  92. }
  93. /**
  94. * 别名
  95. *
  96. * @author 许祖兴 < zuxing.xu@lettered.cn>
  97. * @date 2020/3/16 10:52
  98. *
  99. * @param $field
  100. * @return string
  101. */
  102. public function aliasField($field): string
  103. {
  104. return sprintf('%s.%s', $this->getTable(), $field);
  105. }
  106. /**
  107. * 查询
  108. *
  109. * @author 许祖兴 < zuxing.xu@lettered.cn>
  110. * @date 2020/3/16 10:53
  111. *
  112. * @param array|int $where 主键或者条件数组
  113. * @return mixed
  114. */
  115. public function getBy($where = [])
  116. {
  117. return static::get($where);
  118. }
  119. /**
  120. * 查询All
  121. *
  122. * @author 许祖兴 < zuxing.xu@lettered.cn>
  123. * @date 2020/3/16 10:53
  124. *
  125. * @param array $where
  126. * @return mixed
  127. */
  128. public function getAll($where = [])
  129. {
  130. return static::all($where);
  131. }
  132. /**
  133. * 取值
  134. *
  135. * @author 许祖兴 < zuxing.xu@lettered.cn>
  136. * @date 2020/3/16 10:53
  137. *
  138. * @param $id
  139. * @param $field
  140. * @return mixed
  141. */
  142. public function getValue($id, $field)
  143. {
  144. return static::where($this->getPk(), $id)->value($field);
  145. }
  146. }