97 lines
2.3 KiB
PHP
97 lines
2.3 KiB
PHP
|
|
<?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 logicShowBy(array $where, array $with = [], array $dataLimit = [])
|
|||
|
|
// {
|
|||
|
|
// return CrudUtil::findOneBy($this->modelClass, $where, $with, $dataLimit);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 创建(可带 dataLimit)
|
|||
|
|
*/
|
|||
|
|
public function logicCreate(array $data, array $dataLimit = [])
|
|||
|
|
{
|
|||
|
|
return CrudUtil::create($this->modelClass, $data, $dataLimit);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 主键更新(可带 dataLimit)
|
|||
|
|
*/
|
|||
|
|
// public function logicUpdateByPk($id, array $data, array $dataLimit = [])
|
|||
|
|
// {
|
|||
|
|
// return CrudUtil::updateByPk($this->modelClass, $id, $data, $dataLimit);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 任意条件更新(可带 dataLimit)
|
|||
|
|
*/
|
|||
|
|
public function logicUpdateBy(array $config, array $data)
|
|||
|
|
{
|
|||
|
|
return CrudUtil::updateBy(CrudUtil::buildQuery($config), $data);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 主键删除(批量)
|
|||
|
|
*/
|
|||
|
|
// public function logicDeleteByPk(array $ids)
|
|||
|
|
// {
|
|||
|
|
// return CrudUtil::deleteByPk($this->modelClass, $ids);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 任意条件删除(可带 dataLimit)
|
|||
|
|
*/
|
|||
|
|
public function logicDeleteBy(array $config)
|
|||
|
|
{
|
|||
|
|
return CrudUtil::deleteBy(CrudUtil::buildQuery($config));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 复杂查询回调
|
|||
|
|
*/
|
|||
|
|
public function logicComplex(callable $callback)
|
|||
|
|
{
|
|||
|
|
return CrudUtil::executeComplex($this->modelClass, $callback);
|
|||
|
|
}
|
|||
|
|
}
|