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

90 lines
1.7 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)
{
foreach ($data as $field => $value) {
if (in_array($field, $this->field)) {
$this->{$field} = $value;
}
}
if ($this->save()) {
2019-12-07 17:31:38 +08:00
return $this->{$this->getPk()};
2019-12-06 09:17:40 +08:00
}
return false;
}
/**
*
* @time 2019年12月03日
* @param $id
* @param $data
* @return bool
*/
public function updateBy($id, $data)
{
$model = $this->findBy($id);
foreach ($data as $field => $value) {
if (in_array($field, $this->field)) {
$model->{$field} = $value;
}
}
if ($model->save()) {
2019-12-07 17:31:38 +08:00
return $model->id;
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);
}
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
}
}