扩展功能
This commit is contained in:
73
extend/catcher/traits/db/BaseOptionsTrait.php
Normal file
73
extend/catcher/traits/db/BaseOptionsTrait.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?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()) {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
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()) {
|
||||
$model->id;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月03日
|
||||
* @param $id
|
||||
* @param array $field
|
||||
* @return mixed
|
||||
*/
|
||||
public function findBy($id, array $field = ['*'])
|
||||
{
|
||||
return static::where($this->getPk(), $id)->select($field)->find();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月03日
|
||||
* @param $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function deleteBy($id)
|
||||
{
|
||||
return static::destory($id);
|
||||
}
|
||||
}
|
48
extend/catcher/traits/db/TransTrait.php
Normal file
48
extend/catcher/traits/db/TransTrait.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace catcher\traits\db;
|
||||
|
||||
use think\facade\Db;
|
||||
|
||||
trait TransTrait
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月03日
|
||||
* @return void
|
||||
*/
|
||||
public function startTrans()
|
||||
{
|
||||
Db::startTrans();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月03日
|
||||
* @return void
|
||||
*/
|
||||
public function commit()
|
||||
{
|
||||
Db::commit();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月03日
|
||||
* @return void
|
||||
*/
|
||||
public function rollback()
|
||||
{
|
||||
Db::rollback();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月03日
|
||||
* @param \Closure $function
|
||||
* @return void
|
||||
*/
|
||||
public function transaction(\Closure $function)
|
||||
{
|
||||
Db::transaction($function());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user