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)
|
|
|
|
{
|
2020-04-21 10:51:25 +08:00
|
|
|
$model = parent::create($data, $this->field, true);
|
2019-12-06 09:17:40 +08:00
|
|
|
|
2020-04-21 10:51:25 +08:00
|
|
|
return $model->{$this->getPk()};
|
2019-12-06 09:17:40 +08:00
|
|
|
}
|
|
|
|
|
2020-04-21 10:51:25 +08:00
|
|
|
/**33
|
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);
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:51:25 +08:00
|
|
|
/**
|
|
|
|
* 批量插入
|
|
|
|
*
|
|
|
|
* @time 2020年04月19日
|
|
|
|
* @param array $data
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function insertAllBy(array $data)
|
|
|
|
{
|
|
|
|
$newData = [];
|
|
|
|
foreach ($data as $item) {
|
|
|
|
foreach ($item as $field => $value) {
|
|
|
|
if (!in_array($field, $this->field)) {
|
|
|
|
unset($item[$field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array('created_at', $this->field)) {
|
|
|
|
$item['created_at'] = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array('updated_at', $this->field)) {
|
|
|
|
$item['updated_at'] = time();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$newData[] = $item;
|
|
|
|
}
|
|
|
|
return $this->insertAll($newData);
|
|
|
|
}
|
|
|
|
|
2019-12-07 17:31:38 +08:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|