2025-07-18 17:32:42 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
namespace app\common\traits\crud;
|
|
|
|
|
|
|
|
|
|
|
|
use app\common\utils\CrudUtil;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Trait CrudLogicTrait
|
|
|
|
|
|
*
|
|
|
|
|
|
* 提供最基础的 CRUD 和复杂查询方法,基于 CrudUtil。
|
|
|
|
|
|
* 使用此 Trait 的类,须定义:
|
|
|
|
|
|
* protected string $modelClass = \App\common\model\YourModel::class;
|
|
|
|
|
|
*/
|
|
|
|
|
|
trait CrudLogicTrait
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 分页列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function logicList(array $config, int $limit, int $page)
|
|
|
|
|
|
{
|
|
|
|
|
|
$config['model'] = $this->modelClass;
|
|
|
|
|
|
return CrudUtil::paginate(CrudUtil::buildQuery($config), $limit, $page);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 全量列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function logicListAll(array $config)
|
|
|
|
|
|
{
|
|
|
|
|
|
$config['model'] = $this->modelClass;
|
|
|
|
|
|
return CrudUtil::getAll(CrudUtil::buildQuery($config));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 查询单条
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function logicShow(array $config)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrudUtil::findOneBy(CrudUtil::buildQuery($config));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建(可带 dataLimit)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function logicCreate(array $data, array $dataLimit = [])
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrudUtil::create($this->modelClass, $data, $dataLimit);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 任意条件更新(可带 dataLimit)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function logicUpdateBy(array $config, array $data)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrudUtil::updateBy(CrudUtil::buildQuery($config), $data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 任意条件删除(可带 dataLimit)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function logicDeleteBy(array $config)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrudUtil::deleteBy(CrudUtil::buildQuery($config));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 复杂查询回调
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function logicComplex(callable $callback)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrudUtil::executeComplex($this->modelClass, $callback);
|
|
|
|
|
|
}
|
2025-07-23 17:06:48 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据主键查询
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function findByPk(int $id, array $with = [])
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrudUtil::findByPk($this->modelClass, $id, $with);
|
|
|
|
|
|
}
|
2025-07-18 17:32:42 +08:00
|
|
|
|
}
|