Skip to content

Commit a26dd44

Browse files
committed
Merge branch '5.2' of https://github.com/top-think/framework into 5.2
2 parents 4f1abb3 + 8dfe8eb commit a26dd44

File tree

7 files changed

+143
-227
lines changed

7 files changed

+143
-227
lines changed

src/think/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
class App extends Container
2323
{
24-
const VERSION = '5.2.0beta1';
24+
const VERSION = '5.2.0beta2';
2525

2626
/**
2727
* 应用名称

src/think/Model.php

Lines changed: 60 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ abstract class Model implements JsonSerializable, ArrayAccess
4242
*/
4343
private $force = false;
4444

45+
/**
46+
* 是否Replace
47+
* @var bool
48+
*/
49+
private $replace = false;
50+
4551
/**
4652
* 更新条件
4753
* @var array
@@ -96,12 +102,6 @@ abstract class Model implements JsonSerializable, ArrayAccess
96102
*/
97103
protected static $initialized = [];
98104

99-
/**
100-
* 是否从主库读取(主从分布式有效)
101-
* @var array
102-
*/
103-
protected static $readMaster;
104-
105105
/**
106106
* 查询对象实例
107107
* @var Query
@@ -210,21 +210,6 @@ public function newInstance(array $data = [], bool $isUpdate = false, $where = n
210210
return (new static($data))->isUpdate($isUpdate, $where);
211211
}
212212

213-
/**
214-
* 是否从主库读取数据(主从分布有效)
215-
* @access public
216-
* @param bool $all 是否所有模型有效
217-
* @return $this
218-
*/
219-
public function readMaster(bool $all = false)
220-
{
221-
$model = $all ? '*' : static::class;
222-
223-
static::$readMaster[$model] = true;
224-
225-
return $this;
226-
}
227-
228213
/**
229214
* 创建模型的查询对象
230215
* @access protected
@@ -238,11 +223,7 @@ protected function buildQuery(): Query
238223
$query->model($this)
239224
->name($this->name)
240225
->json($this->json, $this->jsonAssoc)
241-
->setFieldType($this->type);
242-
243-
if (isset(static::$readMaster['*']) || isset(static::$readMaster[static::class])) {
244-
$query->master(true);
245-
}
226+
->setFieldType($this->schema);
246227

247228
// 设置当前数据表和模型名
248229
if (!empty($this->table)) {
@@ -369,6 +350,18 @@ public function isForce(): bool
369350
return $this->force;
370351
}
371352

353+
/**
354+
* 新增数据是否使用Replace
355+
* @access public
356+
* @param bool $replace
357+
* @return $this
358+
*/
359+
public function replace(bool $replace = true)
360+
{
361+
$this->replace = $replace;
362+
return $this;
363+
}
364+
372365
/**
373366
* 设置数据是否存在
374367
* @access public
@@ -395,17 +388,22 @@ public function isExists(): bool
395388
* 保存当前数据对象
396389
* @access public
397390
* @param array $data 数据
398-
* @param array $where 更新条件
399391
* @param string $sequence 自增序列名
400392
* @return bool
401393
*/
402-
public function save(array $data = [], array $where = [], string $sequence = null): bool
394+
public function save(array $data = [], string $sequence = null): bool
403395
{
404-
if (!$this->checkBeforeSave($data, $where)) {
396+
// 数据对象赋值
397+
foreach ($data as $key => $value) {
398+
$this->setAttr($key, $value, $data);
399+
}
400+
401+
// 事件回调
402+
if (false === $this->trigger('before_write')) {
405403
return false;
406404
}
407405

408-
$result = $this->exists ? $this->updateData($where) : $this->insertData($sequence);
406+
$result = $this->exists ? $this->updateData() : $this->insertData($sequence);
409407

410408
if (false === $result) {
411409
return false;
@@ -421,39 +419,6 @@ public function save(array $data = [], array $where = [], string $sequence = nul
421419
return true;
422420
}
423421

424-
/**
425-
* 写入之前检查数据
426-
* @access protected
427-
* @param array $data 数据
428-
* @param array $where 保存条件
429-
* @return bool
430-
*/
431-
protected function checkBeforeSave(array $data, $where): bool
432-
{
433-
if (!empty($data)) {
434-
435-
// 数据对象赋值
436-
foreach ($data as $key => $value) {
437-
$this->setAttr($key, $value, $data);
438-
}
439-
440-
if (!empty($where)) {
441-
$this->exists = true;
442-
$this->updateWhere = $where;
443-
}
444-
}
445-
446-
// 数据自动完成
447-
$this->autoCompleteData($this->auto);
448-
449-
// 事件回调
450-
if (false === $this->trigger('before_write')) {
451-
return false;
452-
}
453-
454-
return true;
455-
}
456-
457422
/**
458423
* 检查数据是否允许写入
459424
* @access protected
@@ -464,10 +429,14 @@ protected function checkAllowFields(array $append = []): array
464429
{
465430
// 检测字段
466431
if (empty($this->field)) {
467-
$query = $this->db();
468-
$table = $this->table ?: $query->getTable();
432+
if ($this->schema) {
433+
$this->field = array_keys($this->schema);
434+
} else {
435+
$query = $this->db();
436+
$table = $this->table ?: $query->getTable();
469437

470-
$this->field = $query->getConnection()->getTableFields($table);
438+
$this->field = $query->getConnection()->getTableFields($table);
439+
}
471440

472441
$field = $this->field;
473442
} else {
@@ -489,13 +458,14 @@ protected function checkAllowFields(array $append = []): array
489458
/**
490459
* 保存写入数据
491460
* @access protected
492-
* @param array $where 保存条件
493461
* @return bool
494462
*/
495-
protected function updateData(array $where): bool
463+
protected function updateData(): bool
496464
{
497465
// 自动更新
498-
$this->autoCompleteData($this->update);
466+
$auto = array_merge($this->auto, $this->update);
467+
468+
$this->autoCompleteData($auto);
499469

500470
// 事件回调
501471
if (false === $this->trigger('before_update')) {
@@ -519,12 +489,8 @@ protected function updateData(array $where): bool
519489
$this->data[$this->updateTime] = $data[$this->updateTime];
520490
}
521491

522-
if (empty($where) && !empty($this->updateWhere)) {
523-
$where = $this->updateWhere;
524-
}
525-
526492
// 检查允许字段
527-
$allowFields = $this->checkAllowFields(array_merge($this->auto, $this->update));
493+
$allowFields = $this->checkAllowFields($auto);
528494

529495
// 保留主键数据
530496
foreach ($this->data as $key => $val) {
@@ -545,14 +511,18 @@ protected function updateData(array $where): bool
545511

546512
if (!empty($array)) {
547513
$where = $array;
514+
} else {
515+
$where = $this->updateWhere;
548516
}
549517

550518
foreach ((array) $this->relationWrite as $name => $val) {
551-
if (is_array($val)) {
552-
foreach ($val as $key) {
553-
if (isset($data[$key])) {
554-
unset($data[$key]);
555-
}
519+
if (!is_array($val)) {
520+
continue;
521+
}
522+
523+
foreach ($val as $key) {
524+
if (isset($data[$key])) {
525+
unset($data[$key]);
556526
}
557527
}
558528
}
@@ -593,7 +563,9 @@ protected function updateData(array $where): bool
593563
protected function insertData(string $sequence = null): bool
594564
{
595565
// 自动写入
596-
$this->autoCompleteData($this->insert);
566+
$auto = array_merge($this->auto, $this->insert);
567+
568+
$this->autoCompleteData($auto);
597569

598570
// 时间戳自动写入
599571
$this->checkTimeStampWrite();
@@ -603,7 +575,7 @@ protected function insertData(string $sequence = null): bool
603575
}
604576

605577
// 检查允许字段
606-
$allowFields = $this->checkAllowFields(array_merge($this->auto, $this->insert));
578+
$allowFields = $this->checkAllowFields($auto);
607579

608580
$db = $this->db();
609581
$db->startTrans();
@@ -644,52 +616,6 @@ protected function insertData(string $sequence = null): bool
644616
}
645617
}
646618

647-
/**
648-
* 字段值(延迟)增长
649-
* @access public
650-
* @param string $field 字段名
651-
* @param integer $step 增长值
652-
* @param integer $lazyTime 延时时间(s)
653-
* @return integer|true
654-
* @throws Exception
655-
*/
656-
public function setInc(string $field, int $step = 1, int $lazyTime = 0)
657-
{
658-
// 读取更新条件
659-
$where = $this->getWhere();
660-
661-
$result = $this->where($where)->setInc($field, $step, $lazyTime);
662-
663-
if (true !== $result) {
664-
$this->data[$field] += $step;
665-
}
666-
667-
return $result;
668-
}
669-
670-
/**
671-
* 字段值(延迟)减少
672-
* @access public
673-
* @param string $field 字段名
674-
* @param integer $step 减少值
675-
* @param integer $lazyTime 延时时间(s)
676-
* @return integer|true
677-
* @throws Exception
678-
*/
679-
public function setDec(string $field, int $step = 1, int $lazyTime = 0)
680-
{
681-
// 读取更新条件
682-
$where = $this->getWhere();
683-
684-
$result = $this->where($where)->setDec($field, $step, $lazyTime);
685-
686-
if (true !== $result) {
687-
$this->data[$field] -= $step;
688-
}
689-
690-
return $result;
691-
}
692-
693619
/**
694620
* 获取当前的更新条件
695621
* @access protected
@@ -735,9 +661,9 @@ public function saveAll(array $dataSet, bool $replace = true): Collection
735661

736662
foreach ($dataSet as $key => $data) {
737663
if ($this->exists || (!empty($auto) && isset($data[$pk]))) {
738-
$result[$key] = self::update($data, [], $this->field);
664+
$result[$key] = self::update($data, $this->field);
739665
} else {
740-
$result[$key] = self::create($data, $this->field);
666+
$result[$key] = self::create($data, $this->field, $this->replace);
741667
}
742668
}
743669

@@ -825,17 +751,18 @@ public function auto(array $fields)
825751
* @access public
826752
* @param array $data 数据数组
827753
* @param array $field 允许字段
754+
* @param bool $replace 使用Replace
828755
* @return static
829756
*/
830-
public static function create(array $data, array $field = []): Model
757+
public static function create(array $data, array $field = [], bool $replace = false): Model
831758
{
832759
$model = new static();
833760

834761
if (!empty($field)) {
835762
$model->allowField($field);
836763
}
837764

838-
$model->isUpdate(false)->save($data);
765+
$model->isUpdate(false)->replace($replace)->save($data);
839766

840767
return $model;
841768
}
@@ -844,19 +771,18 @@ public static function create(array $data, array $field = []): Model
844771
* 更新数据
845772
* @access public
846773
* @param array $data 数据数组
847-
* @param array $where 更新条件
848774
* @param array $field 允许字段
849775
* @return static
850776
*/
851-
public static function update(array $data, array $where = [], array $field = [])
777+
public static function update(array $data, array $field = [])
852778
{
853779
$model = new static();
854780

855781
if (!empty($field)) {
856782
$model->allowField($field);
857783
}
858784

859-
$model->isUpdate(true)->save($data, $where);
785+
$model->isUpdate(true)->save($data);
860786

861787
return $model;
862788
}

src/think/Validate.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,10 @@ protected function checkItem(string $field, $value, $rules, $data, string $title
535535
continue;
536536
}
537537

538-
if ('must' == $info || 0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) {
539-
// 验证类型
540-
$callback = isset(self::$type[$type]) ? self::$type[$type] : [$this, $type];
541-
// 验证数据
542-
$result = call_user_func_array($callback, [$value, $rule, $data, $field, $title]);
538+
if (isset(self::$type[$type])) {
539+
$result = call_user_func_array(self::$type[$type], [$value, $rule, $data, $field, $title]);
540+
} elseif ('must' == $info || 0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) {
541+
$result = call_user_func_array([$this, $type], [$value, $rule, $data, $field, $title]);
543542
} else {
544543
$result = true;
545544
}

0 commit comments

Comments
 (0)