Dao层详解

dao层属于数据库操作层,位于模型之下,services之上,dao专门用来存放,复杂的sql语句操作逻辑。把操作sql的逻辑完全和services层分离出来。复用性极高。适应复杂的开发场景。

dao层的内部方法

用来设置关联模型

abstract protected function setModel(): string;

用来设置链表模型

protected function setJoinModel(): string

用来获取条数,会走搜索器

public function count(array $where = []): int

获取某些条件数据,不会走搜索器

public function selectList(array $where, $field = '*', $page = 0, $limit = 0)

用来获取条数,不会走搜索器

 public function getCount(array $where)

获取某些条件去重总数,可选择是否走搜索器

public function getDistinctCount(array $where, $field, $search = true)

获取当前模型实例化后的句柄

protected function getModel()

获取主键

protected function getPk()

获取一条数据,不走搜索器

public function get($id, ?array $field = [], ?array $with = [])

查询一条数据是否存在,不走搜索器

 public function be($map, string $field = '')

根据条件获取一条数据,不走搜索器

public function getOne(array $where, ?string $field = '*', array $with = [])

获取单个字段值,不走搜索器

public function value(array $where, ?string $field = '')

获取某个字段数组,不走搜索器

public function getColumn(array $where, string $field, string $key = '')

删除,不走搜索器

public function delete($id, ?string $key = null)

更新数据,不走搜索器

public function update($id, array $data, ?string $key = null)

批量更新数据,不走搜索器

public function batchUpdate(array $ids, array $data, ?string $key = null)

插入数据

public function save(array $data)

批量插入数据

public function saveAll(array $data)

获取某个字段内的值,不走搜索器

public function getFieldValue($value, string $filed, ?string $valueKey = '', ?array $where = [])

获取搜索器和搜索条件key

private function getSearchData(array $withSearch)

根据搜索器获取搜索内容

protected function withSearchSelect(array $withSearch, ?array $data = [])

搜索

protected function search(array $where = [])

求和,可选择是否走搜索器

public function sum(array $where, string $field, bool $search = false)

高精度加法

public function bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2)

高精度减法

public function bcDec($key, string $decField, string $dec, string $keyField = null, int $acc = 2)

高精度计算并保存

public function bc($key, string $incField, string $inc, string $keyField = null, int $type = 1, int $acc = 2)

减库存加销量

public function decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')

加库存减销量

public function incStockDecSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales')

dao类层示例

setModel中设置关联的model
引入上一章创建的User模型

namespase app\dao;

use app\dao\BaseDao;
use app\model\User;

class UserDao extends BaseDao
{

    //这里返回模型的类名
     protected function setModel(): string
     {
             return User::class;
     }

}

查询某个条件返回列表数据

namespase app\dao;

use app\dao\BaseDao;
use app\model\User;

class UserDao extends BaseDao
{

    //这里返回模型的类名
     protected function setModel(): string
     {
             return User::class;
     }

     //根据条件查询数据,返回列表
     public function selectList(array $where)
     {
             return $this->getModel()->where($where)->select()->toArray();
     }

}

可以继承SearchDaoTrait类来完成基础的查询

namespase app\dao;

use app\dao\BaseDao;
use app\model\User;
use crmeb\traits\SearchDaoTrait;


class UserDao extends BaseDao
{
    use SearchDaoTrait;

    //这里返回模型的类名
     protected function setModel(): string
     {
             return User::class;
     }

}

可以使用getList方法来查询列表,此函数包含分页、查询条件、排序、模型关联。比较简单的列表查询就可以完全使用这个方法来完成

public function getList(array $where = [], array $field = ['*'], int $page = 0, int $limit = 0, $sort = null, array $with = [])
本页目录