## Repository [TOC] ## 定义 在 system/repository/library 目录中定义仓库类文件如下: ``` namespace system\repository\library; use system\model\User; use houdunwang\model\repository\Repository; class UserRepository extends Repository { function model() { return User::class; } } ``` ### 默认方法 #### 查找所有记录 ``` public function all($columns = ['*']) ``` #### 分页查询 ``` public function paginate($page = 15, $columns = ['*']) ``` #### 新增记录 ``` public function create(array $data) ``` #### 更新记录 ``` public function update(array $data, $id) ``` #### 根据主键删除记录 ``` public function delete($id) ``` #### 按主键查询记录 ``` public function find($id, $columns = ['*']) ``` #### 按指定字段值查询 ``` public function findBy($field, $value, $columns = ['*']) ``` ## 控制器中使用 ``` namespace app\system\controller; use system\repository\library\UserRepository; class Entry extends Controller { public function __construct(UserRepository $userRepository) { p($userRepository->all()); } ```