crmeb多商户二开接口流程文档Dao【3】

  • Dao定义

目录位于

  1. \app\common\dao

具体dao下在分块,根据具体逻辑功能不同划分

dao位于数据模型model层与逻辑处理层repository之间,处理每个特定逻辑数据库操作或有共性的查询、操作等

每一个dao对应一个model,比如在\app\common\model下存在User模型,那么在\app\dao下必然存在一个UserDao,必需继承baseDao并实现getModel方法,关联一个model类

 
  1. protected function getModel(): string
  2. {
  3. return User::class;
  4. }

一个dao操作一个model类,model也只能通过这个dao被使用,不允许跨dao调用以及夸层级调用model

完整定义一个dao

 
  1. <?php
  2. namespace app\dao\user;
  3. use app\dao\BaseDao;
  4. use app\model\user\User;
  5. /**
  6. * 用户
  7. * Class UserDao
  8. * @package app\dao\user
  9. */
  10. class UserDao extends BaseDao
  11. {
  12. protected function getModel(): string
  13. {
  14. return User::class;
  15. }
  16. //公共查询列表
  17. public function search(array $where)
  18. {
  19. return $this->getModel()->getDb()->where('id',$where['id']);
  20. }
  21. }
  • 使用

    repository->dao->model 都是单一对应
    dao必需在对应的repository中调用,不能被其他repository或者dao调用
    比如:UserDao 必然只能被UserServices 使用
    repository中注入对应dao
    ```

public function __construct(UserDao $dao)
{
$this->dao = $dao;
}

 
  1. 在repository中使用

$this->dao

 
  1. 就可以直接调用dao中定义的查询或者增删改差逻辑,比如:

$this->dao->search([‘is_del’=>0]);

 
  1. repository完整使用:

namespace app\common\repositories\user;

use app\common\dao\user\UserDao;

class UserRepository extends BaseRepository
{
protected $dao;
public function __construct(UserDao $dao)
{
$this->dao = $dao;
}

 
  1. public function search(array $where, int $page, int $limit)
  2. {
  3. $query = $this->dao->search($where);
  4. $list = $query->page($page,$limit)->select();
  5. $count =$query->count();
  6. return compact('count','list');
  7. }

}
```

  • baseDao介绍

    baseDao中内置了一些常用查询、聚合、添加、修改方法,如下:
    • @ get(int $id) 获取一条数据(主键)
      返回find()查询结果集对象
    • @ getWhere(array $where, string $field = ‘*’, array $with = [])获取一条数据(多条件)
      返回find()查询结果集对象
    • @ selectWhere(array $where, string $field = ‘*’)获取一条数据(多条件)
      返回select()查询结果集对象
    • @ getWith(int $id, array $with)获取一条数据(关联查询)
      返回find()结果,查询结果集对象
    • @ update($id, array $data) 修改数据
      返回update()结果,影响数据条数
  • @ updates($id,s array $data) 修改数据(多条数据)
    返回update()结果,影响数据条数
    • @ fieldExists($map, string $field = ‘’) 查询一条数据是否存在
      返回true|false
    • @ getWhereCount(array $where = []) 获取某些条件总数(复杂条件嵌套,可以是二位数组)
      返回count()统计条数
    • @ findOrCreate(array $where) 查询某个数据是否存在,如果不存在就创建这条数据
      返回create()结果,当前模型对象实例
    • @ delete($id, ?string $key = null) 删除
      返回delete()结果
    • @ create(array $data) 保存数据
      返回create()结果,当前模型对象实例
    • @ insertAll(array $data) 批量保存数据
      返回insertAll()结果,添加数据条数
    • @ getSearch(array $where) 搜索器,传入where条件,在model中定义搜索器就可查询
      返回select()结果
    • @ incField(int $id, string $field , $num = 1) 条件自增
      返回update()结果
    • @ decField(int $id, string $field , $num = 1)条件自减
      返回update()结果
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crmeb专业二开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值