# 服务
StartCMS服务与系统服务的定义有点不同,这里泛指业务相关的服务,一般继承CMS基础服务start\Service
如需创建在执行框架的某些组件或者功能的时候需要依赖的一些基础服务,一般继承TP的基础服务think\Service
以上两者都不强制继承
关于系统服务的说明请移步TP的服务定义 (opens new window)
关于业务服务的说明请继续往下看
# 服务定义
业务服务的命名规则是模型名+Service,采用驼峰命名并且首字母大小
继承于start\Service
的服务均可调用以下静态方法
- ::model() 获取相关模型
- ::getList() 查询列表数据
- ::getPage() 查询分页数据
- ::getInfo() 查询详情数据
- ::create() 创建记录
- ::udpate() 更新记录
- ::saveAll() 自动创建或更新记录
- ::remove() 删除记录
- ::import() 导入记录
- ::startTrans() 开始事务
- ::startCommit() 提交事务
- ::startRollback() 回滚事务
如果不继承
start\Service
请按需实现相关操作方法
# 服务创建
- 通过命令行创建
php start make:service + 应用名@服务名(无需添加Service后缀)
如:
php start make:service test@Article
# 模型绑定
给服务model属性绑定具体的模型即可完成关联
<?php
namespace app\test\service;
use start\Service;
/**
* @mixin \start\Service
*/
class ArticleService extends Service
{
// 如服务不存在其他应用调用只写模型名也行,但建议统一带上命名空间
public $model = 'app\test\model\Article';
}
# 内置方法
# 创建记录
接收数组参数并返回创建的模型实例,参数格式最好在调用前完成校验,如有其他需求请重写接口
/**
* 创建记录
* @param array $input [description]
* @return object [description]
*/
public static function create($input)
{
// 默认逻辑
$model = self::model();
if ($model->save($input)) {
return $model;
} else {
throw_error('create fail');
}
// 如需进行事务提交可以重写为以下逻辑
$model = self::model()
self::startTrans();
try {
// code...
$model->save($input);
self::startCommit();
} catch (\Exception $e) {
// 注意:如果try内还有throw_error,这里需要捕捉\HttpResponseException错误
self::startRollback();
throw_error($e->getMessage());
}
}
# 更新记录
接收带主键的数组参数,返回更新后的模型实例,参数格式最好在调用前完成校验,如有其他需求请重写接口
/**
* 更新记录
* @param array $input [description]
* @return object [description]
*/
public static function update($input)
{
// 默认逻辑
$model = self::model();
$pk = $model->getPk();
if (!isset($input[$pk]) || empty($input[$pk])) {
throw_error("$pk can not empty");
}
$model = $model->find($input[$pk]);
if ($model->save($input)) {
return $model;
} else {
throw_error('update fail');
}
}
# 删除记录
支持单个删除和批量删除,接收主键数组或,
分割的主键字符串,返回true/false
/**
* 删除记录
* @param array $filter [description]
* @return boolean [description]
*/
public static function remove($filter)
{
// 默认逻辑
if (is_string($filter) && strstr($filter, ',') !== false) {
$filter = explode(',', $filter);
}
$model = self::model();
if (!is_array($filter)) {
return $model->find($filter)->remove();
} else {
$list = $model->where($model->getPk(), 'in', $filter)->select();
foreach ($list as $item) {
$item->remove();
}
return true;
}
}
# 获取详情
接收综合查询 (opens new window)所支持的参数格式,返回查询的模型实例
注: 不传with时会根据model的$with属性进行关联查询,否则按with参数关联,如只查询主表$with传空数组[]就好
/**
* 获取详情
* @param array $filter [description]
* @return object [description]
*/
public static function getInfo($filter, $with = null)
{
// 默认逻辑
$model = self::model();
return $model->info($filter, $with);
}
# 获取列表
接收综合查询 (opens new window)所支持的参数格式,返回查询数据集 (opens new window)
注: 不传with时会根据model的$with属性进行关联查询,否则按with参数关联,如只查询主表$with传空数组[]就好
/**
* 获取列表
* @param array $filter [description]
* @param array $order [description]
* @return collection [description]
*/
public static function getList($filter = [], $order = [], $with = null)
{
$model = self::model();
return $model->list($filter, $order, $with);
}
# 获取分页
接收综合查询 (opens new window)所支持的参数格式,返回查询数据集 (opens new window)
注: 不传with时会根据model的$with属性进行关联查询,否则按with参数关联,如只查询主表$with传空数组[]就好
/**
* 获取分页
* @param array $filter [description]
* @param array $order [description]
* @return collection [description]
*/
public static function getPage($filter = [], $order = [], $with = null)
{
$model = self::model();
return $model->page($filter, $order, $with);
}
# 导入记录
接收数组参数和查重关键字段,返回成功和失败的记录数量
如:keys=['title'],则导入时会根据数据表中title是否存在相同记录来进行更新或创建
/**
* 导入记录
* @param array $list [导入列表]
* @param array $keys [查重字段]
* @return array [成功及失败数量]
*/
public static function import(array $list, array $keys=[])
{
if (!is_array($list)) {
$list = json_decode($list, true);
}
$success = 0; // 成功记录数
$error = 0; // 失败记录数
self::startTrans();
try {
foreach ($list as $item) {
$where = array_reduce($keys, function ($result, $key) use ($item) {
if(isset($item[$key])){
return array_merge($result, [$key => $item[$key]]);
}
return $result;
}, []);
$model = self::getInfo($where);
if ($model) {
$item['is_deleted'] = 0;
$model->save($item) ? $success++ : $error++;
} else {
$model = self::model();
$model->save($item) ? $success++ : $error++;
}
}
self::startCommit();
return compact('success', 'error');
} catch (\Exception $e) {
self::startRollback();
throw_error($e->getMessage());
return false;
}
}
# 导出记录
导出就直接调用getPage或者getList返回就行啦!建议进行分页导出