hyperf-chat/app/Repository/ExampleRepository.php

304 lines
9.0 KiB
PHP
Raw Normal View History

2021-08-27 22:55:42 +08:00
<?php
declare(strict_types=1);
namespace App\Repository;
2021-08-31 22:33:41 +08:00
use App\Helpers\HashHelper;
2021-08-29 22:04:36 +08:00
use App\Model\Talk\TalkRecords;
2021-08-27 22:55:42 +08:00
use App\Model\User;
2021-08-28 23:41:01 +08:00
use Hyperf\Utils\Str;
2021-08-27 22:55:42 +08:00
2021-08-28 23:41:01 +08:00
/**
* Repository 使用案例
*
* @package App\Repository
*/
2021-08-27 22:55:42 +08:00
class ExampleRepository extends BaseRepository
{
public function __construct(User $model)
{
parent::__construct($model);
}
2021-08-28 23:41:01 +08:00
// 自增自减案例 increment decrement
2021-08-28 16:11:38 +08:00
public function case1()
{
2021-09-04 23:20:50 +08:00
// 数据自增
2021-08-28 23:41:01 +08:00
// $this->increment(['id' => 1017], 'is_robot', 4, [
// 'updated_at' => date('Y-m-d H:i:s')
// ]);
2021-08-28 16:11:38 +08:00
2021-09-04 23:20:50 +08:00
// 数据自减
2021-08-28 23:41:01 +08:00
// $this->decrement(['id:gt' => 1017], 'is_robot', 1, [
// 'updated_at' => date('Y-m-d H:i:s')
// ]);
2021-08-28 16:11:38 +08:00
}
2021-08-28 23:41:01 +08:00
// 聚合查询相关案例 count, max, min, avg, sum
2021-08-28 16:11:38 +08:00
public function case2()
{
2021-09-04 23:20:50 +08:00
// 统计总数
// $this->count(['id:gt' => 3000]);
2021-08-28 23:41:01 +08:00
2021-09-04 23:20:50 +08:00
// 最大值
// $this->max(['id:gt' => 3000], 'id');
2021-08-28 23:41:01 +08:00
2021-09-04 23:20:50 +08:00
// 最小值
// $this->min(['id:gt' => 3000], 'id');
2021-08-28 16:11:38 +08:00
2021-09-04 23:20:50 +08:00
// 平均值
// $this->avg(['id:gt' => 3000], 'id');
2021-08-28 16:11:38 +08:00
2021-09-04 23:20:50 +08:00
// 求和
// $this->sum(['id:gt' => 3000], 'id');
2021-08-28 16:11:38 +08:00
}
2021-08-28 23:41:01 +08:00
// model value pluck exists doesntExist
public function case3()
2021-08-27 22:55:42 +08:00
{
2021-08-28 23:41:01 +08:00
// $this->value(['id' => 20540000], 'id');
2021-08-27 22:55:42 +08:00
2021-08-28 23:41:01 +08:00
// $this->pluck(['id:gt' => 1017, 'id:lt' => 1040], 'mobile');
2021-08-27 22:55:42 +08:00
2021-08-28 23:41:01 +08:00
// $this->exists(['id' => 2054]);
2021-08-27 22:55:42 +08:00
2021-08-28 23:41:01 +08:00
// $this->doesntExist(['id' => 2054]);
2021-08-27 22:55:42 +08:00
}
2021-08-28 23:41:01 +08:00
// model 原生方法
public function case4()
2021-08-28 16:11:38 +08:00
{
2021-08-28 23:41:01 +08:00
// 创建一条数据
// $this->create([
// 'mobile' => '135' . mt_rand(1000, 9999) . mt_rand(1000, 9999),
// 'nickname' => Str::random(10),
// 'password' => HashHelper::make('aa123456'),
// 'created_at' => date('Y-m-d H:i:s'),
// 'updated_at' => date('Y-m-d H:i:s'),
// ]);
2021-08-28 16:11:38 +08:00
2021-08-28 23:41:01 +08:00
// 批量创建数据
// $this->insert([
// [
// 'mobile' => '135' . mt_rand(1000, 9999) . mt_rand(1000, 9999),
// 'nickname' => Str::random(10),
// 'password' => HashHelper::make('aa123456'),
// 'created_at' => date('Y-m-d H:i:s'),
// 'updated_at' => date('Y-m-d H:i:s'),
// ],
// [
// 'mobile' => '135' . mt_rand(1000, 9999) . mt_rand(1000, 9999),
// 'nickname' => Str::random(10),
// 'password' => HashHelper::make('aa123456'),
// 'created_at' => date('Y-m-d H:i:s'),
// 'updated_at' => date('Y-m-d H:i:s'),
// ],
// ]);
2021-08-28 16:11:38 +08:00
2021-08-28 23:41:01 +08:00
// 创建一条数据并返回主键ID
// $user_id = $this->insertGetId([
// 'mobile' => '135' . mt_rand(1000, 9999) . mt_rand(1000, 9999),
// 'nickname' => Str::random(10),
// 'password' => HashHelper::make('aa123456'),
// 'created_at' => date('Y-m-d H:i:s'),
// 'updated_at' => date('Y-m-d H:i:s'),
// ]);
// 查询一条数据不存在即新增一条数据
// $user = $this->firstOrCreate([
// 'mobile' => 18698272054,
// ], [
// 'mobile' => 18698272054,
// 'nickname' => Str::random(10),
// 'password' => HashHelper::make('aa123456'),
// 'created_at' => date('Y-m-d H:i:s'),
// 'updated_at' => date('Y-m-d H:i:s'),
// ]);
// 更新一条数据不存在就创建
// $this->updateOrCreate([
// 'mobile' => 18698272054,
// ], [
// 'mobile' => 18698272054,
// 'nickname' => Str::random(10),
// 'password' => HashHelper::make('aa123456'),
// 'created_at' => date('Y-m-d H:i:s'),
// 'updated_at' => date('Y-m-d H:i:s'),
// ]);
2021-08-28 16:11:38 +08:00
2021-08-28 23:41:01 +08:00
// 根据主键ID查询数据
// $this->find(2054, ['id', 'mobile']);
2021-08-28 16:11:38 +08:00
2021-08-28 23:41:01 +08:00
// 主键查询没有就抛出错误
// $this->findOrFail(20540000, ['id', 'mobile']);
// 根据条件更新数据
// $this->update([
// 'id' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
// ], [
// 'gender' => 2,
// 'updated_at' => date('Y-m-d H:i:s'),
// ]);
2021-08-29 23:01:42 +08:00
// 批量更新数据
// $this->batchUpdate([
// 'id:gt' => 2054
// ], [
// 'email' => '', // 不使用条件判断,默认更新
// 'gender' => [
// 'field' => 'id',//判断的字段,可选(不设置默认使用当前字段)
// 'default' => 0, // 默认字段值
// 'filter' => [ // 数据判断
// '2054' => 1,
// '2055' => 2,
// ]
// ],
// ]);
// 批量删除数据
// $this->delete([
// 'id' => 4241
// ]);
2021-08-28 16:11:38 +08:00
}
2021-08-28 23:41:01 +08:00
public function case5()
2021-08-27 22:55:42 +08:00
{
2021-08-28 23:41:01 +08:00
// 根据条件获取满足条件的第一条数据
2021-08-28 16:11:38 +08:00
// $result = $this->first([
2021-08-28 23:41:01 +08:00
// 'id' => 2054,
// ], ['*'], true);
// 根据条件获取所有满足条件的数据
// $this->get([
// 'id' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
// 'gender' => 2
2021-09-04 23:20:50 +08:00
// ], ['id', 'mobile'],[0,10]);
2021-08-28 23:41:01 +08:00
// 分页获取数据
2021-08-29 22:04:36 +08:00
// $data = $this->paginate([
// 'id:gt' => 2054,
// ], ['id', 'mobile'], 1, 5);
//
// var_dump($data);
2021-08-28 23:41:01 +08:00
// 打印查询 sql 语句
// $this->toSql([
// 'id' => 2054,
2021-08-28 16:11:38 +08:00
// 'or' => [
// 'gender' => [1, 2, 3],
// [
// 'id:lt' => 2011,
// 'mobile' => 2066,
// ],
// [
// 'id:gt' => 1344,
// 'mobile' => 1233,
// 'or' => [
// 'nickname' => "1111",
// 'email' => '22222'
// ]
// ],
// ]
// ]);
2021-08-29 22:04:36 +08:00
// 原生 SQL 查询
// $this->sql('SELECT * FROM `lar_users` WHERE id = ?', [2054]);
2021-08-29 23:01:42 +08:00
}
2021-08-29 22:04:36 +08:00
2021-08-29 23:01:42 +08:00
public function other()
{
2021-09-04 23:20:50 +08:00
// 获取 Model 实例
$model = $this->buildWhere();
2021-08-27 22:55:42 +08:00
}
2021-08-28 23:41:01 +08:00
// where 条件查询案例
public function where_case()
{
$where = [
// 等值查询
2021-09-04 23:20:50 +08:00
'mobile' => "18798271234",
'mobile:eq' => "18798271234",
2021-08-28 23:41:01 +08:00
// model 自带操作符查询
['id', '=', 12],
['id', '>', 12],
['id', '>=', 12],// ...
// in 或者 not in 查询
2021-09-04 23:20:50 +08:00
'id:in' => [1, 2, 3],
'id:not in' => [5, 6, 7],
'id:gt' => 10,
'id:lt' => 100,
'mobile:like' => "138%",
'mobile:not like' => "1381%",
// or 查询(可嵌套使用)
'or' => [
2021-08-28 23:41:01 +08:00
'field' => '',
['field', '>', ''],
2021-09-04 23:20:50 +08:00
'or' => [
['field', '>', ''],
],
2021-08-28 23:41:01 +08:00
['field', '>', ''],
2021-09-04 23:20:50 +08:00
],
// 排序
'order by' => [
'created_at' => 'desc',
'updated_at' => 'asc',
'`updated_at - created_at`' => 'desc'
],
// 分组
'group by' => [
'gender', 'is_robot',
],
'having by' => [
['account_id', '>', 100],
],
// 关联查询
'join table' => [
// orm 自带条件
// [$table, $first, $operator = null, $second = null, $type = 'inner']
// 数组方式
[
'users_emoticon', 'users_emoticon.user_id', '=', 'users.id', 'left'
],
// 闭包方法
['users_emoticon', function ($join) {
$join->on('users.id', '=', 'users_emoticon.user_id');
}, null, null, 'left']
2021-08-28 23:41:01 +08:00
]
];
2021-09-04 23:20:50 +08:00
echo $this->buildWhere($where)->toSql();
}
public function where_case2()
{
$this->get([
'id:gt' => 3000,
'id:lt' => 4000,
'id:between' => [3000, 4000],
'or' => [
['mobile', 'like', '138%'],
['mobile', 'like', '139%'],
[
['id', '=', 12],
['id', '=', 13],
'or' => [
['id', '=', 12],
['id', '=', 13],
]
]
],
'is_robot' => 1,
]);
// select * from `lar_users` where `id` > '3000' and `id` < '4000' and `id` between '3000' and '4000' and (`mobile` like '138%' or `mobile` like '139%' or (`id` = '12' and `id` = '13')) and `is_robot` = '1'
2021-08-28 23:41:01 +08:00
}
2021-08-27 22:55:42 +08:00
}