/
BaseQuery.php
1298 lines (1118 loc) · 36.2 KB
/
BaseQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\db;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException as Exception;
use think\db\exception\ModelNotFoundException;
use think\helper\Str;
use think\Model;
use think\Paginator;
/**
* 数据查询基础类
*/
abstract class BaseQuery
{
use concern\TimeFieldQuery;
use concern\AggregateQuery;
use concern\ModelRelationQuery;
use concern\ResultOperation;
use concern\Transaction;
use concern\WhereQuery;
/**
* 当前数据库连接对象
* @var Connection
*/
protected $connection;
/**
* 当前数据表名称(不含前缀)
* @var string
*/
protected $name = '';
/**
* 当前数据表主键
* @var string|array
*/
protected $pk;
/**
* 当前数据表自增主键
* @var string
*/
protected $autoinc;
/**
* 当前数据表前缀
* @var string
*/
protected $prefix = '';
/**
* 当前查询参数
* @var array
*/
protected $options = [];
/**
* 架构函数
* @access public
* @param ConnectionInterface $connection 数据库连接对象
*/
public function __construct(ConnectionInterface $connection)
{
$this->connection = $connection;
$this->prefix = $this->connection->getConfig('prefix');
}
/**
* 利用__call方法实现一些特殊的Model方法
* @access public
* @param string $method 方法名称
* @param array $args 调用参数
* @return mixed
* @throws Exception
*/
public function __call(string $method, array $args)
{
if (strtolower(substr($method, 0, 5)) == 'getby') {
// 根据某个字段获取记录
$field = Str::snake(substr($method, 5));
return $this->where($field, '=', $args[0])->find();
} elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') {
// 根据某个字段获取记录的某个值
$name = Str::snake(substr($method, 10));
return $this->where($name, '=', $args[0])->value($args[1]);
} elseif (strtolower(substr($method, 0, 7)) == 'whereor') {
$name = Str::snake(substr($method, 7));
array_unshift($args, $name);
return call_user_func_array([$this, 'whereOr'], $args);
} elseif (strtolower(substr($method, 0, 5)) == 'where') {
$name = Str::snake(substr($method, 5));
array_unshift($args, $name);
return call_user_func_array([$this, 'where'], $args);
} elseif ($this->model && method_exists($this->model, 'scope' . $method)) {
// 动态调用命名范围
$method = 'scope' . $method;
array_unshift($args, $this);
call_user_func_array([$this->model, $method], $args);
return $this;
} else {
throw new Exception('method not exist:' . static::class . '->' . $method);
}
}
/**
* 创建一个新的查询对象
* @access public
* @return BaseQuery
*/
public function newQuery(): BaseQuery
{
$query = new static($this->connection);
if ($this->model) {
$query->model($this->model);
}
if (isset($this->options['table'])) {
$query->table($this->options['table']);
} else {
$query->name($this->name);
}
if (!empty($this->options['json'])) {
$query->json($this->options['json'], $this->options['json_assoc']);
}
if (isset($this->options['field_type'])) {
$query->setFieldType($this->options['field_type']);
}
return $query;
}
/**
* 获取当前的数据库Connection对象
* @access public
* @return ConnectionInterface
*/
public function getConnection()
{
return $this->connection;
}
/**
* 指定当前数据表名(不含前缀)
* @access public
* @param string $name 不含前缀的数据表名字
* @return $this
*/
public function name(string $name)
{
$this->name = $name;
return $this;
}
/**
* 获取当前的数据表名称
* @access public
* @return string
*/
public function getName(): string
{
return $this->name ?: $this->model->getName();
}
/**
* 获取数据库的配置参数
* @access public
* @param string $name 参数名称
* @return mixed
*/
public function getConfig(string $name = '')
{
return $this->connection->getConfig($name);
}
/**
* 得到当前或者指定名称的数据表
* @access public
* @param string $name 不含前缀的数据表名字
* @return mixed
*/
public function getTable(string $name = '')
{
if (empty($name) && isset($this->options['table'])) {
return $this->options['table'];
}
$name = $name ?: $this->name;
return $this->prefix . Str::snake($name);
}
/**
* 设置字段类型信息
* @access public
* @param array $type 字段类型信息
* @return $this
*/
public function setFieldType(array $type)
{
$this->options['field_type'] = $type;
return $this;
}
/**
* 获取最近一次查询的sql语句
* @access public
* @return string
*/
public function getLastSql(): string
{
return $this->connection->getLastSql();
}
/**
* 获取返回或者影响的记录数
* @access public
* @return integer
*/
public function getNumRows(): int
{
return $this->connection->getNumRows();
}
/**
* 获取最近插入的ID
* @access public
* @param string $sequence 自增序列名
* @return mixed
*/
public function getLastInsID(string $sequence = null)
{
return $this->connection->getLastInsID($this, $sequence);
}
/**
* 得到某个字段的值
* @access public
* @param string $field 字段名
* @param mixed $default 默认值
* @return mixed
*/
public function value(string $field, $default = null)
{
$result = $this->connection->value($this, $field, $default);
$array[$field] = $result;
$this->result($array);
return $array[$field];
}
/**
* 得到某个列的数组
* @access public
* @param string|array $field 字段名 多个字段用逗号分隔
* @param string $key 索引
* @return array
*/
public function column($field, string $key = ''): array
{
$result = $this->connection->column($this, $field, $key);
if (count($result) != count($result, 1)) {
$this->resultSet($result, false);
}
return $result;
}
/**
* 查询SQL组装 union
* @access public
* @param mixed $union UNION
* @param boolean $all 是否适用UNION ALL
* @return $this
*/
public function union($union, bool $all = false)
{
$this->options['union']['type'] = $all ? 'UNION ALL' : 'UNION';
if (is_array($union)) {
$this->options['union'] = array_merge($this->options['union'], $union);
} else {
$this->options['union'][] = $union;
}
return $this;
}
/**
* 查询SQL组装 union all
* @access public
* @param mixed $union UNION数据
* @return $this
*/
public function unionAll($union)
{
return $this->union($union, true);
}
/**
* 指定查询字段
* @access public
* @param mixed $field 字段信息
* @return $this
*/
public function field($field)
{
if (empty($field)) {
return $this;
} elseif ($field instanceof Raw) {
$this->options['field'][] = $field;
return $this;
}
if (is_string($field)) {
if (preg_match('/[\<\'\"\(]/', $field)) {
return $this->fieldRaw($field);
}
$field = array_map('trim', explode(',', $field));
}
if (true === $field) {
// 获取全部字段
$fields = $this->getTableFields();
$field = $fields ?: ['*'];
}
if (isset($this->options['field'])) {
$field = array_merge((array) $this->options['field'], $field);
}
$this->options['field'] = array_unique($field);
return $this;
}
/**
* 指定要排除的查询字段
* @access public
* @param array|string $field 要排除的字段
* @return $this
*/
public function withoutField($field)
{
if (empty($field)) {
return $this;
}
if (is_string($field)) {
$field = array_map('trim', explode(',', $field));
}
// 字段排除
$fields = $this->getTableFields();
$field = $fields ? array_diff($fields, $field) : $field;
if (isset($this->options['field'])) {
$field = array_merge((array) $this->options['field'], $field);
}
$this->options['field'] = array_unique($field);
return $this;
}
/**
* 指定其它数据表的查询字段
* @access public
* @param mixed $field 字段信息
* @param string $tableName 数据表名
* @param string $prefix 字段前缀
* @param string $alias 别名前缀
* @return $this
*/
public function tableField($field, string $tableName, string $prefix = '', string $alias = '')
{
if (empty($field)) {
return $this;
}
if (is_string($field)) {
$field = array_map('trim', explode(',', $field));
}
if (true === $field) {
// 获取全部字段
$fields = $this->getTableFields($tableName);
$field = $fields ?: ['*'];
}
// 添加统一的前缀
$prefix = $prefix ?: $tableName;
foreach ($field as $key => &$val) {
if (is_numeric($key) && $alias) {
$field[$prefix . '.' . $val] = $alias . $val;
unset($field[$key]);
} elseif (is_numeric($key)) {
$val = $prefix . '.' . $val;
}
}
if (isset($this->options['field'])) {
$field = array_merge((array) $this->options['field'], $field);
}
$this->options['field'] = array_unique($field);
return $this;
}
/**
* 设置数据
* @access public
* @param array $data 数据
* @return $this
*/
public function data(array $data)
{
$this->options['data'] = $data;
return $this;
}
/**
* 去除查询参数
* @access public
* @param string $option 参数名 留空去除所有参数
* @return $this
*/
public function removeOption(string $option = '')
{
if ('' === $option) {
$this->options = [];
$this->bind = [];
} elseif (isset($this->options[$option])) {
unset($this->options[$option]);
}
return $this;
}
/**
* 指定查询数量
* @access public
* @param int $offset 起始位置
* @param int $length 查询数量
* @return $this
*/
public function limit(int $offset, int $length = null)
{
$this->options['limit'] = $offset . ($length ? ',' . $length : '');
return $this;
}
/**
* 指定分页
* @access public
* @param int $page 页数
* @param int $listRows 每页数量
* @return $this
*/
public function page(int $page, int $listRows = null)
{
$this->options['page'] = [$page, $listRows];
return $this;
}
/**
* 指定当前操作的数据表
* @access public
* @param mixed $table 表名
* @return $this
*/
public function table($table)
{
if (is_string($table)) {
if (strpos($table, ')')) {
// 子查询
} elseif (false === strpos($table, ',')) {
if (strpos($table, ' ')) {
[$item, $alias] = explode(' ', $table);
$table = [];
$this->alias([$item => $alias]);
$table[$item] = $alias;
}
} else {
$tables = explode(',', $table);
$table = [];
foreach ($tables as $item) {
$item = trim($item);
if (strpos($item, ' ')) {
[$item, $alias] = explode(' ', $item);
$this->alias([$item => $alias]);
$table[$item] = $alias;
} else {
$table[] = $item;
}
}
}
} elseif (is_array($table)) {
$tables = $table;
$table = [];
foreach ($tables as $key => $val) {
if (is_numeric($key)) {
$table[] = $val;
} else {
$this->alias([$key => $val]);
$table[$key] = $val;
}
}
}
$this->options['table'] = $table;
return $this;
}
/**
* 指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc'])
* @access public
* @param string|array|Raw $field 排序字段
* @param string $order 排序
* @return $this
*/
public function order($field, string $order = '')
{
if (empty($field)) {
return $this;
} elseif ($field instanceof Raw) {
$this->options['order'][] = $field;
return $this;
}
if (is_string($field)) {
if (!empty($this->options['via'])) {
$field = $this->options['via'] . '.' . $field;
}
if (strpos($field, ',')) {
$field = array_map('trim', explode(',', $field));
} else {
$field = empty($order) ? $field : [$field => $order];
}
} elseif (!empty($this->options['via'])) {
foreach ($field as $key => $val) {
if (is_numeric($key)) {
$field[$key] = $this->options['via'] . '.' . $val;
} else {
$field[$this->options['via'] . '.' . $key] = $val;
unset($field[$key]);
}
}
}
if (!isset($this->options['order'])) {
$this->options['order'] = [];
}
if (is_array($field)) {
$this->options['order'] = array_merge($this->options['order'], $field);
} else {
$this->options['order'][] = $field;
}
return $this;
}
/**
* 分页查询
* @access public
* @param int|array $listRows 每页数量 数组表示配置参数
* @param int|bool $simple 是否简洁模式或者总记录数
* @return Paginator
* @throws Exception
*/
public function paginate($listRows = null, $simple = false): Paginator
{
if (is_int($simple)) {
$total = $simple;
$simple = false;
}
$defaultConfig = [
'query' => [], //url额外参数
'fragment' => '', //url锚点
'var_page' => 'page', //分页变量
'list_rows' => 15, //每页数量
];
if (is_array($listRows)) {
$config = array_merge($defaultConfig, $listRows);
$listRows = intval($config['list_rows']);
} else {
$config = $defaultConfig;
$listRows = intval($listRows ?: $config['list_rows']);
}
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = $page < 1 ? 1 : $page;
$config['path'] = $config['path'] ?? Paginator::getCurrentPath();
if (!isset($total) && !$simple) {
$options = $this->getOptions();
unset($this->options['order'], $this->options['limit'], $this->options['page'], $this->options['field']);
$bind = $this->bind;
$total = $this->count();
if ($total > 0) {
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select();
} else {
if (!empty($this->model)) {
$results = new \think\model\Collection([]);
} else {
$results = new \think\Collection([]);
}
}
} elseif ($simple) {
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select();
$total = null;
} else {
$results = $this->page($page, $listRows)->select();
}
$this->removeOption('limit');
$this->removeOption('page');
return Paginator::make($results, $listRows, $page, $total, $simple, $config);
}
/**
* 根据数字类型字段进行分页查询(大数据)
* @access public
* @param int|array $listRows 每页数量或者分页配置
* @param string $key 分页索引键
* @param string $sort 索引键排序 asc|desc
* @return Paginator
* @throws Exception
*/
public function paginateX($listRows = null, string $key = null, string $sort = null): Paginator
{
$defaultConfig = [
'query' => [], //url额外参数
'fragment' => '', //url锚点
'var_page' => 'page', //分页变量
'list_rows' => 15, //每页数量
];
$config = is_array($listRows) ? array_merge($defaultConfig, $listRows) : $defaultConfig;
$listRows = is_int($listRows) ? $listRows : (int) $config['list_rows'];
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = $page < 1 ? 1 : $page;
$config['path'] = $config['path'] ?? Paginator::getCurrentPath();
$key = $key ?: $this->getPk();
$options = $this->getOptions();
if (is_null($sort)) {
$order = $options['order'] ?? '';
if (!empty($order)) {
$sort = $order[$key] ?? 'desc';
} else {
$this->order($key, 'desc');
$sort = 'desc';
}
} else {
$this->order($key, $sort);
}
$newOption = $options;
unset($newOption['field'], $newOption['page']);
$data = $this->newQuery()
->options($newOption)
->field($key)
->where(true)
->order($key, $sort)
->limit(1)
->find();
$result = $data[$key];
if (is_numeric($result)) {
$lastId = 'asc' == $sort ? ($result - 1) + ($page - 1) * $listRows : ($result + 1) - ($page - 1) * $listRows;
} else {
throw new Exception('not support type');
}
$results = $this->when($lastId, function ($query) use ($key, $sort, $lastId) {
$query->where($key, 'asc' == $sort ? '>' : '<', $lastId);
})
->limit($listRows)
->select();
$this->options($options);
return Paginator::make($results, $listRows, $page, null, true, $config);
}
/**
* 根据最后ID查询更多N个数据
* @access public
* @param int $limit LIMIT
* @param int|string $lastId LastId
* @param string $key 分页索引键 默认为主键
* @param string $sort 索引键排序 asc|desc
* @return array
* @throws Exception
*/
public function more(int $limit, $lastId = null, string $key = null, string $sort = null): array
{
$key = $key ?: $this->getPk();
if (is_null($sort)) {
$order = $this->getOptions('order');
if (!empty($order)) {
$sort = $order[$key] ?? 'desc';
} else {
$this->order($key, 'desc');
$sort = 'desc';
}
} else {
$this->order($key, $sort);
}
$result = $this->when($lastId, function ($query) use ($key, $sort, $lastId) {
$query->where($key, 'asc' == $sort ? '>' : '<', $lastId);
})->limit($limit)->select();
$last = $result->last();
$result->first();
return [
'data' => $result,
'lastId' => $last ? $last[$key] : null,
];
}
/**
* 查询缓存
* @access public
* @param mixed $key 缓存key
* @param integer|\DateTime $expire 缓存有效期
* @param string|array $tag 缓存标签
* @return $this
*/
public function cache($key = true, $expire = null, $tag = null)
{
if (false === $key || !$this->getConnection()->getCache()) {
return $this;
}
if ($key instanceof \DateTimeInterface || $key instanceof \DateInterval || (is_int($key) && is_null($expire))) {
$expire = $key;
$key = true;
}
$this->options['cache'] = [$key, $expire, $tag];
return $this;
}
/**
* 指定查询lock
* @access public
* @param bool|string $lock 是否lock
* @return $this
*/
public function lock($lock = false)
{
$this->options['lock'] = $lock;
if ($lock) {
$this->options['master'] = true;
}
return $this;
}
/**
* 指定数据表别名
* @access public
* @param array|string $alias 数据表别名
* @return $this
*/
public function alias($alias)
{
if (is_array($alias)) {
$this->options['alias'] = $alias;
} else {
$table = $this->getTable();
$this->options['alias'][$table] = $alias;
}
return $this;
}
/**
* 设置从主服务器读取数据
* @access public
* @param bool $readMaster 是否从主服务器读取
* @return $this
*/
public function master(bool $readMaster = true)
{
$this->options['master'] = $readMaster;
return $this;
}
/**
* 设置是否严格检查字段名
* @access public
* @param bool $strict 是否严格检查字段
* @return $this
*/
public function strict(bool $strict = true)
{
$this->options['strict'] = $strict;
return $this;
}
/**
* 设置自增序列名
* @access public
* @param string $sequence 自增序列名
* @return $this
*/
public function sequence(string $sequence = null)
{
$this->options['sequence'] = $sequence;
return $this;
}
/**
* 设置JSON字段信息
* @access public
* @param array $json JSON字段
* @param bool $assoc 是否取出数组
* @return $this
*/
public function json(array $json = [], bool $assoc = false)
{
$this->options['json'] = $json;
$this->options['json_assoc'] = $assoc;
return $this;
}
/**
* 指定数据表主键
* @access public
* @param string|array $pk 主键
* @return $this
*/
public function pk($pk)
{
$this->pk = $pk;
return $this;
}
/**
* 查询参数批量赋值
* @access protected
* @param array $options 表达式参数
* @return $this
*/
protected function options(array $options)
{
$this->options = $options;
return $this;
}
/**
* 获取当前的查询参数
* @access public
* @param string $name 参数名
* @return mixed
*/
public function getOptions(string $name = '')
{
if ('' === $name) {
return $this->options;
}
return $this->options[$name] ?? null;
}
/**
* 设置当前的查询参数
* @access public
* @param string $option 参数名
* @param mixed $value 参数值
* @return $this
*/
public function setOption(string $option, $value)
{
$this->options[$option] = $value;
return $this;
}
/**
* 设置当前字段添加的表别名
* @access public
* @param string $via 临时表别名
* @return $this
*/
public function via(string $via = '')
{
$this->options['via'] = $via;
return $this;
}
/**
* 保存记录 自动判断insert或者update
* @access public
* @param array $data 数据
* @param bool $forceInsert 是否强制insert
* @return integer
*/
public function save(array $data = [], bool $forceInsert = false)
{
if ($forceInsert) {
return $this->insert($data);
}
$this->options['data'] = array_merge($this->options['data'] ?? [], $data);
if (!empty($this->options['where'])) {
$isUpdate = true;
} else {
$isUpdate = $this->parseUpdateData($this->options['data']);
}
return $isUpdate ? $this->update() : $this->insert();
}
/**
* 插入记录
* @access public
* @param array $data 数据
* @param boolean $getLastInsID 返回自增主键
* @return integer|string
*/
public function insert(array $data = [], bool $getLastInsID = false)
{
if (!empty($data)) {
$this->options['data'] = $data;
}
return $this->connection->insert($this, $getLastInsID);
}
/**
* 插入记录并获取自增ID
* @access public
* @param array $data 数据
* @return integer|string
*/
public function insertGetId(array $data)
{
return $this->insert($data, true);
}
/**
* 批量插入记录
* @access public
* @param array $dataSet 数据集
* @param integer $limit 每次写入数据限制
* @return integer
*/