catchAdmin/extend/catcher/traits/db/BaseOptionsTrait.php

103 lines
1.8 KiB
PHP
Raw Normal View History

2019-12-06 09:17:40 +08:00
<?php
namespace catcher\traits\db;
trait BaseOptionsTrait
{
/**
*
* @time 2019年12月03日
* @param array $data
* @return bool
*/
public function storeBy(array $data)
{
2019-12-12 09:14:08 +08:00
if ($this->allowField($this->field)->save($data)) {
2019-12-07 17:31:38 +08:00
return $this->{$this->getPk()};
2019-12-06 09:17:40 +08:00
}
return false;
}
2020-01-17 15:49:42 +08:00
/**
*
* @time 2019年12月03日
* @param $id
* @param $data
* @param string $field
* @return bool
*/
public function updateBy($id, $data, $field = ''): bool
2019-12-06 09:17:40 +08:00
{
2020-01-17 15:49:42 +08:00
if (static::update($data, [$field ? : $this->getPk() => $id], $this->field)) {
2019-12-12 09:14:08 +08:00
return true;
2019-12-06 09:17:40 +08:00
}
return false;
}
/**
*
* @time 2019年12月03日
* @param $id
* @param array $field
2019-12-07 17:31:38 +08:00
* @param bool $trash
2019-12-06 09:17:40 +08:00
* @return mixed
*/
2019-12-07 17:31:38 +08:00
public function findBy($id, array $field = ['*'], $trash = false)
2019-12-06 09:17:40 +08:00
{
2019-12-07 17:31:38 +08:00
if ($trash) {
return static::onlyTrashed()->find($id);
}
2019-12-26 09:03:19 +08:00
2019-12-07 17:31:38 +08:00
return static::where($this->getPk(), $id)->field($field)->find();
2019-12-06 09:17:40 +08:00
}
/**
*
* @time 2019年12月03日
* @param $id
2019-12-07 17:31:38 +08:00
* @param $force
* @return mixed
*/
public function deleteBy($id, $force = false)
{
return static::destroy($id, $force);
}
/**
* @time 2019年12月07日
* @param $id
2019-12-06 09:17:40 +08:00
* @return mixed
*/
2019-12-07 17:31:38 +08:00
public function recover($id)
2019-12-06 09:17:40 +08:00
{
2019-12-07 17:31:38 +08:00
return static::onlyTrashed()->find($id)->restore();
2019-12-06 09:17:40 +08:00
}
2020-01-13 21:24:12 +08:00
/**
* 获取删除字段
*
* @time 2020年01月13日
* @return mixed
*/
public function getDeleteAtField()
{
return $this->deleteTime;
}
/**
* 别名
*
* @time 2020年01月13日
* @param $field
* @return string
*/
public function aliasField($field): string
{
return sprintf('%s.%s', $this->getTable(), $field);
}
2019-12-06 09:17:40 +08:00
}