hyperf-chat/app/Repository/ExampleRepository.php

119 lines
2.7 KiB
PHP
Raw Normal View History

2021-08-27 22:55:42 +08:00
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Model\User;
class ExampleRepository extends BaseRepository
{
public function __construct(User $model)
{
parent::__construct($model);
}
2021-08-28 16:46:16 +08:00
public function insert()
{
2021-08-28 16:11:38 +08:00
}
public function case1()
{
$this->increment(['id' => 1017], 'is_robot', 4, [
'updated_at' => date('Y-m-d H:i:s')
]);
$this->decrement(['id:gt' => 1017], 'is_robot', 1, [
'updated_at' => date('Y-m-d H:i:s')
]);
}
public function case2()
{
$res = $this->pluck(['id:gt' => 1017, 'id:lt' => 1040], 'id');
var_dump($this->doesntExist([
2021-08-28 16:46:16 +08:00
'id' => 2054
2021-08-28 16:11:38 +08:00
]));
}
2021-08-27 22:55:42 +08:00
public function where_case()
{
$where = [
// 等值查询
'mobile' => "18798271234",
'mobile:eq' => "18798271234",
// model 自带操作符查询
['id', '=', 12],
['id', '>', 12],
['id', '>=', 12],// ...
// in 或者 not in 查询
'id:in' => [1, 2, 3],
'id' => [1, 2, 3],
'id:not in' => [5, 6, 7],
'id:gt' => 10,
'id:lt' => 100,
'or' => [
'field' => '',
['field', '>', ''],
],
[
'field' => '',
['field', '>', ''],
]
];
}
2021-08-28 16:11:38 +08:00
// 聚合查询相关案例 count, max, min, avg, sum
public function aggregation_case()
{
var_dump('count : ' . $this->count([
'id:gt' => 3000
]));
var_dump('max : ' . $this->max([
'id:gt' => 3000
], 'id'));
var_dump('min : ' . $this->min([
'id:gt' => 3000
], 'id'));
var_dump('avg : ' . $this->avg([
'id:gt' => 3000
], 'id'));
var_dump('sum : ' . $this->sum([
'id:gt' => 3000
], 'id'));
}
2021-08-27 22:55:42 +08:00
// get 查询案例
public function get_case()
{
2021-08-28 16:11:38 +08:00
// $result = $this->first([
2021-08-28 16:46:16 +08:00
// // '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-27 22:55:42 +08:00
}
}