| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace app\http;
- use think\Db;
- trait DbOperation
- {
- /**
- * 存储
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:52
- *
- * @param array $data
- * @return bool
- */
- public function storeBy(array $data)
- {
- if ($this->allowField($this->field)->save($data)) {
- return $this->{$this->getPk()};
- }
- return false;
- }
- /**
- * 更新
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:52
- *
- * @param $id
- * @param $data
- * @param string $field
- * @return bool
- */
- public function updateBy($id, $data, $field = ''): bool
- {
- if (static::update($data, [$field ? : $this->getPk() => $id], $this->field)) {
- return true;
- }
- return false;
- }
- /**
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:52
- *
- * @param $id
- * @param array $field
- * @param bool $trash
- * @return mixed
- */
- public function findBy($id, array $field = ['*'], $trash = false)
- {
- if ($trash) {
- return static::onlyTrashed()->find($id);
- }
- return static::where($this->getPk(), $id)->field($field)->find();
- }
- /**
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:52
- *
- * @param $id
- * @param bool $force
- * @return mixed
- */
- public function deleteBy($id, $force = false)
- {
- return static::destroy($id, $force);
- }
- /**
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:52
- *
- * @param $id
- * @return mixed
- */
- public function recover($id)
- {
- return static::onlyTrashed()->find($id)->restore();
- }
- /**
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:52
- *
- * @return mixed
- */
- public function getDeleteAtField()
- {
- return $this->deleteTime;
- }
- /**
- * 别名
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:52
- *
- * @param $field
- * @return string
- */
- public function aliasField($field): string
- {
- return sprintf('%s.%s', $this->getTable(), $field);
- }
- /**
- * 查询
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:53
- *
- * @param array|int $where 主键或者条件数组
- * @return mixed
- */
- public function getBy($where = [])
- {
- return static::get($where);
- }
- /**
- * 查询All
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:53
- *
- * @param array $where
- * @return mixed
- */
- public function getAll($where = [])
- {
- return static::all($where);
- }
- /**
- * 取值
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 10:53
- *
- * @param $id
- * @param $field
- * @return mixed
- */
- public function getValue($id, $field)
- {
- return static::where($this->getPk(), $id)->value($field);
- }
- }
|